DOM manipulation in Angular 2/4 applications
To manipulate the DOM in Angular 2/4 applications, we need to implement the ngAfterViewInit() AfterViewInit . The ngAfterViewInit() method is called when the bindings of child directives were checked for the first time. In other words, when the view is initially displayed.
@ViewChild provides access to nativeElement . It is recommended that you do not have access to nativeElement inside ngAfterViewInit() , because it is not protected by the browser. In addition, it is not supported by web workers. Web workers will never know when the DOM is updated.
The correct way is to use renderer . The renderer must be inserted into the component constructor. We need to provide the id link to the HTML element in the view like this:
<p #p1></p>
It should be accessed by the corresponding copy of the .ts , something like this:
export class SampleComponent implements AfterViewInit { @ViewChild("p1") p1; constructor(private renderer: Renderer2)
xameeramir Aug 14 '17 at 19:39 on 2017-08-14 19:39
source share