Javascript _ngcontent-c# 在 Angular 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45082129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:54:01  来源:igfitidea点击:

What does _ngcontent-c# mean in Angular?

javascriptangular

提问by Maddy

I'm learning Angular 2/4 and I see the html tags with the ng generated attributes: _ngcontent-c0, _ngcontent-c1...

我正在学习 Angular 2/4,我看到带有 ng 生成属性的 html 标签: _ngcontent-c0, _ngcontent-c1...

What does this c value mean?

这个c值是什么意思?

采纳答案by Max Koretskyi

_ngcontent-c#attributes are added when you use ViewEncapsulation.Emulated- which is default. Angular uses these attributes to target specific elements with the styles. The number cis sort of a unique identifier of the host component. For example, if you have two components with the following templates:

_ngcontent-c#使用时会添加属性ViewEncapsulation.Emulated- 这是默认设置。Angular 使用这些属性来定位具有样式的特定元素。该数字c是宿主组件的唯一标识符。例如,如果您有两个具有以下模板的组件:

ComponentA
<span></span>
<comp-b></comp-b>

ComponenB
<h1></h1>

Angular will mark all elements with styles inside component Aas _ngcontent-c0and all elements with styles inside component Bwith _ngcontent-c1:

角将迎来与内部组件样式的所有元素A_ngcontent-c0和与内部组件样式的所有元素B_ngcontent-c1

<comp-a>
    <span _ngcontent-c0></span>
    <comp-b _ngcontent-c0>
        <h1 _ngcontent-c1></h1>
    </comp-b>
</comp-a>

回答by asifaftab87

you can disable it by adding below import to your component,

您可以通过在组件中添加以下导入来禁用它,

import {ViewEncapsulation} from '@angular/core';

import { Component, OnInit } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class DashboardComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

please note this line :

请注意这一行:

 encapsulation: ViewEncapsulation.None

make no addition of dynamic attribute from angular

不从角度添加动态属性