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
6
votes
1 answer

Angular2 : Call method/ngoninit when parameter of route changes

Right now I'm making a kind of web shop, and on my home page I've a dropdown of category Let say parent cat A, and sub cats B and C. Cat A, B and C are all represented by 1 component, CatAComponent. When I go in the first time in one of those cats,…
TanguyB
  • 1,954
  • 3
  • 21
  • 40
5
votes
3 answers

AG Grid : gridReady event not triggered when loading grid in a component via ComponentFactoryResolver

On my Angular 10 app, I'm loading through the component factory resolver a component containing an AG Grid. When I trigger the factory resolver through a button, everything works fine. The Grid is displayed and the gridReady event is fired…
Pascal Paulis
  • 277
  • 4
  • 17
5
votes
1 answer

Angular 2+ - run same code on ALL ngOnInit automatically

Maybe the answer to this question is relevant for AngularJS (1.x) also, but my question here is for Angular 2 and above. As we know, every component file has its own ngOnInit function, which runs the code inside of it when the component…
TheCuBeMan
  • 1,954
  • 4
  • 23
  • 35
5
votes
1 answer

Angular 4 Data in Service, passing to component(s)

Have some data in a service, and was working perfectly when I had data in an object sitting on the service, but now that I've hooked up a database connection, the data never makes it to the component. I want the service to subscribe to the data…
Chris Rutherford
  • 1,592
  • 3
  • 22
  • 58
4
votes
1 answer

How to use showLoadingOverlay in ag grid during ngoninit

I am using ag grid to display table in one of my application. I could not able to see the loading message when data is getting loaded during the application initialization. Below is my code, ngOnInit() { this.gridOptions = { columnDefs:…
User123
  • 793
  • 4
  • 10
  • 28
4
votes
1 answer

Angular 7 unit testing dependency injection not working in ngOnInit

I am using Angular 7 with and Angular CLI version 7.3.4. I am unit testing a component with 2 service injections all important code is included below. I included the stub which is causing errors, the spec file, and the important part of the…
4
votes
1 answer

Cannot read property of undefined error even though the binding is working

So, I have a dropdown in my web app that I hooked up to my service api call that will populate the dropdown with list of locations. I created the service and set up the Observable using httpclient. Then I call the service to fetch the results of…
Euridice01
  • 2,510
  • 11
  • 44
  • 76
4
votes
1 answer

ngOnInit does not run on error page component

Angular 4 application. I am trying to make an error page in order to show some information about the unhandled exception that may occur. The GlobalErrorHandler intercepts eventual errors and redirects the user to a page consisting of a single…
Albert
  • 201
  • 1
  • 7
4
votes
0 answers

Throwing error in Angular2 ngOnInit does not work as expected

When I have an error happen inside ngOnInit() that error seems to be bubbled up with an TypeError: this._unregisterListenersFn is not a function error instead. Example: constructor() { throw Error('error in constructor'); } gives expected result…
jonas
  • 1,592
  • 3
  • 20
  • 29
3
votes
2 answers

How do I implement OnInit without getting error: Class is using Angular features but is not decorated. Please add an explicit Angular decorator

I would like to get rid of the Error above. It started to appear after re-installed dependencies using npm i. I tried removing "implements onInit" and it disappeared So the problem is related to it. But I want to use "implements onInit" without…
Uri Gross
  • 484
  • 1
  • 8
  • 20
3
votes
2 answers

Why is it not a good practice to write initialization logic in the constructor in angular 2

Why do we write initialization logic only in the OnInit() method and not in constructor method? What are all the side effects of using constructor method for initialization? Please explain.
3
votes
1 answer

ngOnInit is faster than @Input

I have a situation that i m passing a value from a father component to child component with @Input. The problem is that i dont get the value from father before ngOnInit() triggered in the child component. So what happens is that when ngOnInit()…
tlq
  • 887
  • 4
  • 10
  • 21
3
votes
1 answer

Angular5 Service call not working on ngOnInit

When I navigate to results component the service inside ngOnInit works as expected. Than I open the side menu, navigate to another page, then I navigate to the results page again. But this time the page don't render the results.The ng-template is…
Rafael Natal
  • 41
  • 1
  • 6
3
votes
1 answer

call initialize method in ngOnInit or constructor

Im very new to Angular 4, Can you tell me what is the different between call a method like initializeMyObj() in constructor than ngOnInit in ts file? Thank you
user3477485
  • 123
  • 1
  • 8
3
votes
4 answers

Angular 2 ngOnInit is not called when routerlink changes

I have a menu bar that contain a list of menus loaded from express API and every menu is referenced to a page that have an alias which will be the URL of that page. I am trying to load the page when I click to menu, it's done but only when I refresh…
Ilyes Atoui
  • 474
  • 2
  • 9
  • 29
1
2
3
15 16