[英]Angular 2 *ngFor error: Can't bind to 'menuitemtype' since it isn't a known property of 'div'
I created an attribute called data-menuitemtype
that I'm using in a div
inside of an *ngFor
loop. 我在*ngFor
循环内的div
创建了一个称为data-menuitemtype
的属性。 I've never had issues before with creating attributes like this because it is perfectly valid HTML5 syntax, but for some reason *ngFor
loops don't seem to like it and give me the following error: 创建这样的属性之前,我从来没有遇到过问题,因为它是完全有效的HTML5语法,但是由于某种原因, *ngFor
循环似乎并不喜欢它,并给我以下错误:
"Can't bind to 'menuitemtype' since it isn't a known property of 'div'". “无法绑定到'menuitemtype',因为它不是'div'的已知属性”。
If I change it to id
it "works", but then I will have a bunch of id's with the same value which obviously isn't ideal. 如果我将其更改为id
则它“有效”,但是然后我将获得一堆具有相同值的id,这显然不理想。 How can I use created attributes within an *ngFor
loop without causing errors? 如何在*ngFor
循环中使用创建的属性而不会导致错误? Is this possible? 这可能吗?
Here is my html: 这是我的html:
<div *ngFor="let variety of varieties">
<div class="varietyTypeName menu-item-type-header" data-menuitemtype="{{variety.VarietyTypeName}}">
{{variety.VarietyTypeName}}
<i class="fa fa-angle-down arrow-toggle"></i>
</div>
</div>
From what I can see, data-menuitemtype
is not a property, but an attribute and therefore you should use [attr.insertYourAttributeHere]
in your code, like this: 据我[attr.insertYourAttributeHere]
, data-menuitemtype
不是属性,而是属性,因此您应该在代码中使用[attr.insertYourAttributeHere]
,如下所示:
<div *ngFor="let variety of varieties">
<div class="varietyTypeName menu-item-type-header" [attr.data-menuitemtype]="variety.VarietyTypeName">
{{variety.VarietyTypeName}}
<i class="fa fa-angle-down arrow-toggle"></i>
</div>
</div>
Or if you prefer interpolation: 或者,如果您喜欢插值:
<div *ngFor="let variety of varieties">
<div class="varietyTypeName menu-item-type-header" attr.data-menuitemtype="{{variety.VarietyTypeName}}">
{{variety.VarietyTypeName}}
<i class="fa fa-angle-down arrow-toggle"></i>
</div>
</div>
For more information about difference between attributes and properties, check Angular 2 Template Syntax . 有关属性和属性之间差异的更多信息,请检查Angular 2模板语法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.