angular-lifecycle-hooks

What is the difference between ngOnInit(), ngAfterViewInit(), ngafterContentInit(), ngAfterViewChecked() and a constructor()?

做~自己de王妃 提交于 2020-01-22 14:11:46
问题 What is the difference between ngOnInit (), ngAfterViewInit (), ngafterContentInit (), ngAfterViewChecked () and a constructor ()? How do we implement them in the Angular 2? What are their purposes and usages? Where all will it be useful for implementing them? Thanks. 回答1: Those are life cycle hooks that you can tap into to perform operations and different times of a components life cycle. There is an excellent guide on the topic in the official angular documentation: https://angular.io/guide

Angular 7: ChangeDetectorRef detectChanges() causes infinite loop when called from inside a subscription

ⅰ亾dé卋堺 提交于 2019-12-31 06:53:33
问题 I am posting here after reading all the material related to change detection and similar post and failing to solve my problem. ChangeDetectorRef detectChanges() causes infinite loop when called from inside a subscription. If I don't call detectChanges , I get ExpressionChangedAfterCheck error. I seriously don't know how to solve, and why is the ExpressionChangedAfterCheck error comes ngAfterViewInit() { this.route.params.subscribe((params) => { this.userId = params.userId; if (this.userId !=

Angular 7: ChangeDetectorRef detectChanges() causes infinite loop when called from inside a subscription

ぃ、小莉子 提交于 2019-12-31 06:53:07
问题 I am posting here after reading all the material related to change detection and similar post and failing to solve my problem. ChangeDetectorRef detectChanges() causes infinite loop when called from inside a subscription. If I don't call detectChanges , I get ExpressionChangedAfterCheck error. I seriously don't know how to solve, and why is the ExpressionChangedAfterCheck error comes ngAfterViewInit() { this.route.params.subscribe((params) => { this.userId = params.userId; if (this.userId !=

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

How to call ngOnInit() again in Angular 2?

萝らか妹 提交于 2019-11-27 16:06:13
问题 Please explain in this code, how to call ngOnInit() again when I call another method? ngOnInit(): void { this.route.params.subscribe((params: Params) => { this.model=this.userData; }); } update() { this.loading = true; this.userService.update(this.model) .subscribe( data => { alert ('Update successful'); }, error => { alert ('Not updated'); this.loading = false; }); this.user_data(); } 回答1: There are two options from my point of view: Calling ngOnInit() from another function scope . But I