0
如何添加|删除角元素中的css类?如何添加|删除角元素中的css类?
访问角元素是通过:this.elementRef.nativeElement
(import { ElementRef } from '@angular/core';
)。 CSS类名是myclass
。
如何添加|删除角元素中的css类?如何添加|删除角元素中的css类?
访问角元素是通过:this.elementRef.nativeElement
(import { ElementRef } from '@angular/core';
)。 CSS类名是myclass
。
constructor(public elementRef: ElementRef, private renderer: Renderer)
{
this.renderer.setElementClass(this.elementRef, 'class');
// or
this.elementRef.nativeElement.classList.add('class');
}
您可以使用HostBinding要做到这一点,而无需使用渲染器或ElementRef。看到这个例子:
import {Component, HostBinding} from "@angular/core";
@Component({
...
})
export class myComponent {
@HostBinding('class.myclass') visible: boolean = false; // True when visible
}