如何使用Angular编写数据属性? [英] How can I write data attributes using Angular?

查看:54
本文介绍了如何使用Angular编写数据属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的template中使用data attribute时,像这样:

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崩溃:

例外:模板解析错误:由于以下原因,无法绑定到"sectionvalue" 它不是已知的原生属性("

EXCEPTION: Template parse errors: Can't bind to 'sectionvalue' since it isn't a known native property ("

我显然缺少语法,请帮忙.

I'm obviously missing something with the syntax, please help.

推荐答案

改为使用属性绑定语法

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

另请参见:

  • 如何在Angular 2中添加条件属性?
  • See also :

    • How to add conditional attribute in Angular 2?
    • 这篇关于如何使用Angular编写数据属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆