Angular2 — ngAfterViewChecked is called many times..How to call a method just once after DOM is completely loaded?

霸气de小男生 提交于 2019-12-08 15:59:02

问题


I need to apply jquery plugin to radio buttons in Angular2 using Typescript.

If I assign in ngAfterViewChecked, it is called many times and the control is refreshed multiple times.

What is the alternate solution for calling the javascript method after DOM is ready?


回答1:


Try ngAfterViewInit and have a look here.




回答2:


ngAfterViewChecked() would be invoked once the DOM tree get any change.

So if the DOM tree got change for many times, the ngAfterViewChecked() method would be invoked many times.

Suggest not to put business logic in this method. But only the screen refresh related logic instead, like to scroll the window to bottom if new message is coming in.




回答3:


I have been using ngAfterViewChecked when I depend on the DOM being ready.

import { Component, AfterViewChecked } from '@angular/core'; 

export class NavBar implements AfterViewChecked{

   ngAfterViewChecked(){
      // jquery code here
   }
}


来源:https://stackoverflow.com/questions/41391939/angular2-ngafterviewchecked-is-called-many-times-how-to-call-a-method-just-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!