Questions tagged [ngoninit]

For questions about ngoninit, a lifecycle hook that is called after Angular has initialized all data-bound properties of a directive.

ngOnInit is a callback method that is invoked immediately after the default change detector has checked the directive's data-bound properties for the first time, and before any of the view or content children have been checked. It is invoked only once when the directive is instantiated to handle any additional initialization tasks.

See also, the documentation for the interface OnInit

230 questions
1467
votes
27 answers

Difference between Constructor and ngOnInit

Angular provides life cycle hook ngOnInit by default. Why should ngOnInit be used, if we already have a constructor?
Haseena P A
  • 16,858
  • 4
  • 18
  • 35
145
votes
16 answers

Router Navigate does not call ngOnInit when same page

I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need to add anything else ?
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
109
votes
3 answers

What's the difference between ngOnInit and ngAfterViewInit of Angular2?

I can not understand what the difference between ngOnInit and ngAfterViewInit. I found the only difference between them is @ViewChild. According to the following code, the elementRef.nativeElement in them are the same. What scene should we use…
Zhiyuan Sun
  • 1,171
  • 2
  • 7
  • 5
94
votes
3 answers

Angular 2 Component Constructor Vs OnInit

If I want function x to happen every time a component loads, whether its the first time, I navigate to a different site and navigate back or it's the fifth time the component has loaded. What should I put function x in? The component constructor or…
ClickThisNick
  • 5,110
  • 9
  • 44
  • 69
84
votes
24 answers

Why is ngOnInit called twice?

I trying to create new component, ResultComponent, but its ngOnInit() method is getting called twice and I don't know why this is happening. In the code, ResultComponent inherits @Input from the parent component mcq-component. Here is the code:…
Sagar Ganesh
  • 2,454
  • 3
  • 20
  • 32
36
votes
1 answer

Angular 2 ngOnInit not called

I am building an Angular 2 app with version beta.8. In this app i have a component which implements OnInit. In this component i have the function ngOnInit, but the ngOnInit function is never called. import { Component, OnInit } from…
Vincent Kenbeek
  • 583
  • 1
  • 5
  • 11
24
votes
8 answers

Angular 4 ngOnInit not called after router.navigate

I have 3 tabs in which one tab shows a table with list of employees. Works good when it is loaded the first time.ngOnInit Fetches data from server using http get. After that when I click add new employee to open a form, which take input from user…
Viral Patel
  • 485
  • 1
  • 5
  • 11
16
votes
1 answer

Is calling a function on ngOnInit async?

If I call a function in ngOnInit() that makes an observable call to get data, is the this.getSomething() call in ngOnInit still async or does ngOnInit wait until this.getSomething() returns a result? Basically does "doSomethingElse" get executed in…
Felix
  • 610
  • 2
  • 9
  • 21
15
votes
2 answers

Where to subscribe to an observable, constructor or ngoninit in Angular

I know this is posted all over SO and the internet but I'm reading so many different things, I'm just a little confused now. 2 QUESTIONS - Where is the best place to subscribe to my component, the constructor(), or in NgOnInit()? Should I use a…
chuckd
  • 13,460
  • 29
  • 152
  • 331
15
votes
2 answers

Trigger valueChange with Initialized value - angular2

I'm writing an angular2 application and I'm stuck with something. First of all, I have a select which is bind to a formControl : export class MyComponent implements OnInit { profilesBy: Observable; myControl = new FormControl(); …
ByJC
  • 213
  • 1
  • 3
  • 9
14
votes
2 answers

Angular2 routing issue and ngOnInit called twice

I'm having a very odd issue with Angular 2 routing to where my ngOnInit in the component that I am routing to is getting called twice, and the route in the browser is getting reset to the original route. I have a NotificationListComponent and a…
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102
12
votes
2 answers

@Viewchild not initializing during ngOnInit

I'm running two MatTables in different components with data sources from different observables. One of my tables sort functionality is working fine and but on my second table it seems as if the @ViewChild for MatSort doesn't initialize during…
Timotronadon
  • 315
  • 1
  • 2
  • 15
9
votes
5 answers

What is the difference between Angular ngOnInit() and ngOnChanges()?

Angular provides lifecycle hook ngOnInit() and ngOnChanges() by default. Why should ngOnInit be used, if we already have a ngOnChanges? And constructor also.
Chanaka Amarasinghe
  • 3,489
  • 11
  • 26
  • 38
9
votes
1 answer

angular5 - child component called before parent ngOnInit finish

first, make table setting data in ngOnInit() and set to settingA variable. this settingA variable binds to setting in html. and I want to make table in child component using settingA. but child component ngOnInit is called before parent component's…
8
votes
1 answer

ExpressionChangedAfterItHasBeenCheckedError when add validator in ngOnInit

I am trying to add a custom validator to an input, but when I do so it triggers an ExpressionChangedAfterItHasBeenCheckedError error saying something changed from TRUE to FALSE. I traced the problem down to the line below: ngOnInit(): void { …
TSG
  • 4,242
  • 9
  • 61
  • 121
1
2 3
15 16