Change element class in ViewChild in Angular 2

I am using ViewChild in Angular 2 to isolate an element. I can directly manipulate styles, but I can’t find the documentation if you can change the element's style class.

This is a sample code:

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'; } … } 

See if this is possible and how. Any help was appreciated.

+6
source share
1 answer

It seems that you are looking for the className property:

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

Element.className

+8
source

Source: https://habr.com/ru/post/1015253/


All Articles