Angular2 - Add class to item on click

后端 未结 5 399
爱一瞬间的悲伤
爱一瞬间的悲伤 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:56

    If I am understanding the question properly, I believe you can also use the render from angular2 to get a similar code to your example code. For my own project I did the following:

    In my template I have:

  • Local
  • In my component I then have:

    import {Renderer} from '@angular/core';
    //other imports
    
    export class SignupComponent implements OnInit {
    
          constructor(private render:Renderer) { }
    
          userTypeSelect(event:any){
            event.preventDefault()
            this.render.setElementClass(event.target,"active",false);
          }
    
    }
    

    It is worth noting though that I am not using this for a list of items, however I believe it should still work.

    Reference to the Renderer: Renderer Api Docs

提交回复
热议问题