2017-02-24 86 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