2017-02-24 72 views
6

我在Angular 2中使用ViewChild隔离元素。我可以直接操作样式,但我无法找到是否有可能更改元素样式类的文档。更改Angular 2中ViewChild的元素类

这是示例代码:

export class HomeComponent implements OnInit { 

    @ViewChild('slideBg') el:ElementRef; 

    ngAfterViewInit { 
     // style can be changed 
     this.el.nativeElement.style.background = 'red'; 
     // does not work: 
     this.el.nativeElement.class = 'myCSSclass'; 
    } 

    … 

} 

看看是否这是可能的,以及如何。任何帮助赞赏。

回答

7

好像你正在寻找className属性:

this.el.nativeElement.className = 'myCSSclass'; 

Element.className

+0

这是略有不同的对我来说:'this.el.getNativeElement()的className = 'myCSSclass';' – akz92