简体   繁体   中英

Custom attributes return null when logged in angular 2

template
<button type="button"
        (click)="leaderBoard($event)"
        attr.leaders = "individual"
        [class.active]="individualLeaderboard">
   <label>INDIVIDUALS</label>
</button>
<button type="button"
        (click)="leaderBoard($event)"
         attr.leaders = "team"
         [class.active]="teamLeaderboard">
   <label>TEAMS</label>

.ts File

leaderBoard($event) {
    console.log($event.target.getAttribute('attr.leaders'))
 }

I'm simply trying to log the value of the custom attribute when each element is clicked. It works fine if I only have one button, but returns null if there are multiple buttons. Can someone explain the issue to me?

Change your html to:

<button type="button" (click)="leaderBoard($event)"
 leaders='individual' 
 [class.active]="individualLeaderboard">
 <label>INDIVIDUALS</label> 
</button> 

.. and in your ts:

leaderBoard($event){
console.log($event.currentTarget.getAttribute('leaders'));
 }

Would you replace your ts code with:

leaderBoard($event) {
    console.log($event.target.getAttribute('attr.leaders'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM