Angular ngClass and click event for toggling class

后端 未结 7 1322
粉色の甜心
粉色の甜心 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:38

    ngClass should be wrapped in square brackets as this is a property binding. Try this:

    <div class="my_class" (click)="clickEvent($event)"  [ngClass]="{'active': toggle}">                
         Some content
    </div>
    

    In your component:

         //define the toogle property
         private toggle : boolean = false;
    
        //define your method
        clickEvent(event){
           //if you just want to toggle the class; change toggle variable.
           this.toggle = !this.toggle;       
        }
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-02 18:39

    Angular6 using the renderer2 without any variables and a clean template:

    template:

    <div (click)="toggleClass($event,'testClass')"></div>
    

    in ts:

    toggleClass(event: any, class: string) {
      const hasClass = event.target.classList.contains(class);
    
      if(hasClass) {
        this.renderer.removeClass(event.target, class);
      } else {
        this.renderer.addClass(event.target, class);
      }
    }
    

    One could put this in a directive too ;)

    0 讨论(0)
  • 2020-12-02 18:46

    This should work for you.

    In .html:

    <div class="my_class" (click)="clickEvent()"  
        [ngClass]="status ? 'success' : 'danger'">                
         Some content
    </div>
    

    In .ts:

    status: boolean = false;
    clickEvent(){
        this.status = !this.status;       
    }
    
    0 讨论(0)
  • 2020-12-02 18:48

    Instead of having to create a function in the ts file you can toggle a variable from the template itself. You can then use the variable to apply a specific class to the element. Like so-

    <div (click)="status=!status"  
        [ngClass]="status ? 'success' : 'danger'">                
         Some content
    </div>
    

    So when status is true the class success is applied. When it is false danger class is applied.

    This will work without any additional code in the ts file.

    0 讨论(0)
  • 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:`<a (click)="onClick()"
                             [class.selected]="wasClicked">Link</a>
        `)
    export class MyComponent {
        wasClicked = false;
    
        onClick() {
            this.wasClicked= !this.wasClicked;
        }
    }
    
    0 讨论(0)
  • We can also use ngClass to assign multiple CSS classes based on multiple conditions as below:

    <div
      [ngClass]="{
      'class-name': trueCondition,
      'other-class': !trueCondition
    }"
    ></div>
    
    0 讨论(0)
提交回复
热议问题