簡體   English   中英

在Angular 2中更改ViewChild上的元素類

[英]Change element class on ViewChild in Angular 2

我在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';
    }

    …

}

想看看這是否可行以及如何實現。 任何幫助贊賞。

好像你在尋找className屬性:

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

Element.className

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM