2017-07-08 35 views

回答

2
constructor(public elementRef: ElementRef, private renderer: Renderer) 
{ 
     this.renderer.setElementClass(this.elementRef, 'class'); 
    // or 

      this.elementRef.nativeElement.classList.add('class'); 

} 
  1. 使用渲染
  2. 本土元素的使用addClass
1

您可以使用HostBinding要做到这一点,而无需使用渲染器或ElementRef。看到这个例子:

import {Component, HostBinding} from "@angular/core"; 

@Component({ 
    ... 
}) 
export class myComponent { 
    @HostBinding('class.myclass') visible: boolean = false; // True when visible 
} 
相关问题