我觉得我错过了什么。当我尝试在我的模板中使用数据属性时,像这样:

<ol class="viewer-nav">
    <li *ngFor="#section of sections" data-sectionvalue="{{ section.value }}">
        {{ section.text }}
    </li>
</ol>

Angular 2会崩溃:

例外:模板解析错误:不能绑定到'sectionvalue',因为 它不是已知的本地财产(” ] data-sectionvalue = "{{部分。值}}">{{section。文本}}

我显然在语法上遗漏了一些东西,请帮助。


请使用属性绑定语法

<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>

参见:

如何在Angular 2中添加条件属性?

关于访问

<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)
}