问题
I feel like I am missing something. When I try to use a data attribute in my template, like this:
<ol class="viewer-nav">
<li *ngFor="#section of sections" data-value="{{ section.value }}">
{{ section.text }}
</li>
</ol>
Angular 2 crashes with:
EXCEPTION: Template parse errors: Can't bind to 'sectionvalue' since it isn't a known native property ("
]data-sectionvalue="{{ section.value }}">{{ section.text }}
I'm obviously missing something with the syntax, please help.
回答1:
Use attribute binding syntax instead
<ol class="viewer-nav"><li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value">{{ section.text }}</li>
</ol>
or
<ol class="viewer-nav"><li *ngFor="let section of sections"
attr.data-sectionvalue="{{section.value}}">{{ section.text }}</li>
</ol>
See also :
- How to add conditional attribute in Angular 2?
回答2:
About access
<ol class="viewer-nav">
<li *ngFor="let section of sections"
[attr.data-sectionvalue]="section.value"
(click)="get_data($event)">
{{ section.text }}
</li>
</ol>
And
get_data(event) {
console.log(event.target.dataset.sectionvalue)
}
来源:https://stackoverflow.com/questions/34542619/how-can-i-write-data-attributes-using-angular