简体   繁体   English

Angular渲染的DOM html自定义属性

[英]Angular rendered DOM html custom attributes

I am new to frontend and i began using Angular. 我是前端的新手,我开始使用Angular。

When I look to the final DOM i see code like this 当我查看最终的DOM时,我看到这样的代码

<style>
.pane[_ngcontent-c0]~
....
}
</style>

<div _ngcontent-c0 class="pane" ...

This is using CSS attribute selectors but my question is related with the custom attribute "_ngcontent-c0", in html5 there is a new feature with the name of "data custom attributes that are prefixed with (data-*) name. In this case the custom attribute is not using the data- syntax , this does not make the document invalid ?Is it possible to define custom attributes without the data- prefix ? 这是使用CSS属性选择器,但我的问题与自定义属性“ _ngcontent-c0”有关,在html5中,有一个新功能,其名称为“以(data- *)名称为前缀的数据自定义属性。”自定义属性未使用数据语法,这不会使文档无效吗?是否可以在没有数据前缀的情况下定义自定义属性?

Thanks in advance Best regards 预先感谢问候

You are correct that this is not valid HTML anymore because _ngcontent-c0 is not specified in the HTML spec. 您是正确的,这不再是有效的HTML,因为在HTML规范中未指定_ngcontent-c0 But this just means that the attribute is not defined. 但这仅意味着未定义属性。

You can always add custom attributes but these will most likely not have effects because the browser just ignores them. 您始终可以添加自定义属性,但是这些属性很可能不会起作用,因为浏览器只会忽略它们。 Be aware that this might change in the future. 请注意,这将来可能会改变。

What these custom attributes are used for is the encapsulation of CSS styles. 这些自定义属性的用途是CSS样式的封装。 In CSS you can still use these custom attributes for style definitions and the styles of one component will not bleed into the styles of another component. 在CSS中,您仍然可以将这些自定义属性用于样式定义,并且一个组件的样式不会渗入另一组件的样式。 This is basically a workaround for the Shadow DOM. 这基本上是Shadow DOM的一种解决方法。 Angular automatically adds these custom attributes to all styles of a component to encapsulate its styles. Angular会自动将这些自定义属性添加到组件的所有样式以封装其样式。

What you see there: 您在这里看到的是:

_ngcontent-c0 Is Angulars way of creating a scoped DOM. _ngcontent-c0是Angulars创建范围DOM的方法。 They inject these attributes for different reasons, one of them eg for component-scoped styles. 他们注入这些属性的原因是多种多样的,其中之一例如是针对组件范围的样式。

.pane[_ngcontent-c0] <-- This is a CSS selector. .pane[_ngcontent-c0] <-这是CSS选择器。 Each CSS rule in the comonents style sheet is scoped to it. comonents样式表中的每个CSS规则都适用于它。 It happens in preprocessing. 它发生在预处理中。

There is very little human-readable docs on this, this is the best I could find: 关于此的可读文档很少,这是我能找到的最好的文档:

https://medium.com/claritydesignsystem/ng-content-the-hidden-docs-96a29d70d11b https://medium.com/claritydesignsystem/ng-content-the-hidden-docs-96a29d70d11b

You should have no influence on these but the CLI also should not cut anything silently. 您不应对此产生任何影响,但CLI也不应默默地削减任何内容。

Rather the CLI would probably stop compiling due to template syntax error. 相反,由于模板语法错误,CLI可能会停止编译。

By creating a component you create custom HTML tags (kinda). 通过创建组件,您可以创建自定义HTML标签(kinda)。 I mean <app-component> and such. 我的意思是<app-component>类的。 They are the root of a component scope for DOM elements (once again, not 100% accurate, this is more to visualize it). 它们是DOM元素的组件作用域的根(再次,不是100%准确,这更加形象化)。

If you want to create custom attributes there are a few ways: 如果要创建自定义属性,可以采用以下几种方法:

This is the way how you manipulate HTML attributes in Angular (and not only for custom ones) 这是在Angular中操作HTML属性的方式(不仅适用于自定义属性)

This adds or removes the attribute from the element 这会从元素中添加或删除属性

<input [attr.disabled]="!value ? null : '' "

Will result in <input disabled=''> which is the same as <input disabled> . 将导致<input disabled=''><input disabled>相同。 In case we have a value present it would be simply <input> . 如果我们有一个值存在,那就只是<input>

And when using [attr. 而当使用[attr. syntax there is I think no limit to the names you can use, outside of what is allowed by Angular. 在Angular允许的范围之外,我认为可以使用的名称没有任何限制。

To make data-attributes : 制作data-attributes

<div [attr.data-attr-test]=" 'Foo' ">

And simply using a value from the component: 并简单地使用组件中的值:

<img [src]="value">

There are also Directives which are placed like attributes but used to manipulate DOM and do behavioural changes: 还有一些指令,它们像属性一样放置,但用于操纵DOM并进行行为更改:

https://angular.io/guide/attribute-directives https://angular.io/guide/attribute-directives

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM