Angular2 - Add class to item on click

后端 未结 5 415
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 17:18

I have a bunch of list items and would like to highlight each one once it\'s clicked. This is easy for me to do in jQuery or even JavaScript but I\'m lost when it comes to Angul

5条回答
  •  情话喂你
    2021-02-01 17:57

    There are many ways to achieve this, and all are very simple.

    Examples:

  • {{n}}
  • select(item) { this.selected = item; }; isActive(item) { return this.selected === item; };

    Only using html

  • {{n}}
  • Add class on click and remove if we click on the same

    select(item) {
       this.selected = (this.selected === item ? null : item); 
    };
    

    Only using html

  • {{n}}
  • More info

提交回复
热议问题