325

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-sectionvalue="{{ 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.

2 Answers 2

667

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 :

6
  • 27
    how do i access the value of the data attribute?
    – Sean W
    Commented Nov 10, 2016 at 8:46
  • 5
    Please create a new question with some code that demonstrates what you try to accomplish. Commented Nov 10, 2016 at 8:46
  • 12
    This should be Googles #1 answer for the Can't bind to '' since it isn't a known property of '' query. This comment might help a bit...
    – netzaffin
    Commented Jul 18, 2017 at 9:21
  • 2
    @netzaffin This is a useful answer but I've ran into the can't bind error many many times and this is the first time this was the actual problem/solution. Commented Apr 16, 2020 at 18:32
  • How do I bind a callback function to data-* attributes too?
    – Devmaleeq
    Commented Mar 30, 2021 at 19:15
60

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.