Angular ngClass and click event for toggling class

后端 未结 7 1343
粉色の甜心
粉色の甜心 2020-12-02 18:30

In Angular, I would like to use ngClass and click event to toggle class. I looked through online but some are angular1 and there isn\'t any clear instruction or

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 18:51

    So normally you would create a backing variable in the class and toggle it on click and tie a class binding to the variable. Something like:

    @Component(
        selector:'foo',
        template:`Link
        `)
    export class MyComponent {
        wasClicked = false;
    
        onClick() {
            this.wasClicked= !this.wasClicked;
        }
    }
    

提交回复
热议问题