Angular add class dynamically

前端 未结 3 1248
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 04:05

Is there a way to add a class from the .ts file, using Angular solutions

I want to d

3条回答
  •  遥遥无期
    2021-02-08 04:32

    Use Renderer

    See here:https://angular.io/api/core/Renderer

    and here:https://angular.io/api/core/Renderer#setElementClass

    import { Renderer } from '@angular/core';
    
    constructor(private render:Renderer) { }
    
    ChangeScreen(event:any) {
     this.renderer.setElementClass(event.target,"selected",true);
    }
    

    In html:

    
    

    Or Render2:

    See here:https://angular.io/api/core/Renderer2

    and here:https://angular.io/api/core/Renderer2#addClass

    import { Renderer2 } from '@angular/core';
    
    constructor(private render:Renderer2) { }
    
    ChangeScreen(event:any) {
     this.renderer.addClass(event.target,"selected");
    }
    

    In html:

    
    

提交回复
热议问题