如何使用 Angular 编写数据属性?

问题描述 投票:0回答:2

我感觉我失去了一些东西。当我尝试在我的

data
中使用
attribute
template
时,如下所示:

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

Angular 2
崩溃时:

异常:模板解析错误:无法绑定到“sectionvalue”,因为 它不是已知的本地属性(“

]data-sectionvalue =“{{section.value}}”>{{section.text}}

我显然遗漏了一些语法,请帮忙。

angular angular2-template
2个回答
638
投票

改用属性绑定语法

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

另请参阅:


56
投票

关于访问

<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)
}
© www.soinside.com 2019 - 2024. All rights reserved.