How can I write data attributes using Angular?

Serj Sagan picture Serj Sagan · Dec 31, 2015 · Viewed 121.5k times · Source

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

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Dec 31, 2015

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 :