如何使用 Angular 写入数据属性?
How can I write data attributes using Angular?
我觉得我错过了什么。当我尝试在我的 template
中使用 data
attribute
时,像这样:
<ol class="viewer-nav">
<li *ngFor="#section of sections" data-sectionvalue="{{ section.value }}">
{{ section.text }}
</li>
</ol>
Angular 2
崩溃:
EXCEPTION: Template parse errors: Can't bind to 'sectionvalue' since
it isn't a known native property ("
]data-sectionvalue="{{ section.value }}">{{ section.text }}
我显然在语法上遗漏了一些东西,请帮忙。
改用属性绑定语法
<ol class="viewer-nav"><li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value">{{ section.text }}</li>
</ol>
或
<ol class="viewer-nav"><li *ngFor="let section of sections"
attr.data-sectionvalue="{{section.value}}">{{ section.text }}</li>
</ol>
另见:
关于访问
<ol class="viewer-nav">
<li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value"
(click)="get_data($event)">
{{ section.text }}
</li>
</ol>
和
get_data(event) {
console.log(event.target.dataset.sectionvalue)
}
我觉得我错过了什么。当我尝试在我的 template
中使用 data
attribute
时,像这样:
<ol class="viewer-nav">
<li *ngFor="#section of sections" data-sectionvalue="{{ section.value }}">
{{ section.text }}
</li>
</ol>
Angular 2
崩溃:
EXCEPTION: Template parse errors: Can't bind to 'sectionvalue' since it isn't a known native property ("
]data-sectionvalue="{{ section.value }}">{{ section.text }}
我显然在语法上遗漏了一些东西,请帮忙。
改用属性绑定语法
<ol class="viewer-nav"><li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value">{{ section.text }}</li>
</ol>
或
<ol class="viewer-nav"><li *ngFor="let section of sections"
attr.data-sectionvalue="{{section.value}}">{{ section.text }}</li>
</ol>
另见:
关于访问
<ol class="viewer-nav">
<li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value"
(click)="get_data($event)">
{{ section.text }}
</li>
</ol>
和
get_data(event) {
console.log(event.target.dataset.sectionvalue)
}