source

Angular를 사용하여 데이터 속성을 작성하려면 어떻게 해야 합니까?

ittop 2023. 4. 27. 22:44
반응형

Angular를 사용하여 데이터 속성을 작성하려면 어떻게 해야 합니까?

뭔가 놓치고 있는 것 같아요.사용하려고 할 때data attribute내 안에서template다음과 같이:

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

Angular 2충돌 대상:

예외: 템플릿 구문 분석 오류: 알려진 기본 속성이 아니므로 '섹션 값'에 바인딩할 수 없습니다(").

]data-section value="{{segment.value}}">{{segment.텍스트 }}

구문에 분명히 뭔가가 빠져 있습니다. 도와주세요.

대신 특성 바인딩 구문 사용

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

참고 항목:

액세스 정보

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

언급URL : https://stackoverflow.com/questions/34542619/how-can-i-write-data-attributes-using-angular

반응형