First of all, let me start by reading documents, some articles, the ng-book chapter, etc. I still donβt know how it works.
With that said, consider the following:
import { Component, ViewChild, ElementRef } from '@angular/core'
@Component({
selector: 'home',
template: `
<div>Test</div>
<input type="text"#testElem>
<input type="text"#testElem2>
`
})
export class HomeComponent{
@ViewChild('testElem') el:ElementRef;
@ViewChild('testElem2') el2:ElementRef;
ngAfterViewInit() {
this.el.nativeElement.style.background = "red";
this.el.nativeElement.style.background = "blue";
}
}
Plunker
Why is my first element painted blue and the second element is not painted at all?
source
share