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
3
votes
3 answers

Angular 4: under ngOnInit() method, unable to get the value of form element. 'get' is undefined error is coming

Description: Angular 4, Reactive Approach, trying to compare the field values by creating a validator function and passing that function under the ngOnInit() method. Please find my code below, import { Component, OnInit } from…
Shr4N
  • 435
  • 3
  • 18
3
votes
1 answer

How do I run a function on ngOnInit() only once and not again upon returning back from another routerLink?

HomeComponent ngOnInit() { console.log('loaded'); this.retrieveData(); } retrieveData() { // this.dataService.getData().subscribe(...); } I am retrieving data when the component loads. When the user clicks on another routerLink, for…
anonym
  • 4,460
  • 12
  • 45
  • 75
3
votes
0 answers

How can I trigger ngOnChanges only after every input values has been set in ngOnInit

How can I trigger ngOnChanges only after every input values has been set in ngOnInit. My code --> `ngOnInit() { this.multiSelectorData.forEach(item=>{ this.allItems.push(item.data); }); } ngOnChanges(changes: SimpleChanges) { …
RemyaJ
  • 5,358
  • 4
  • 22
  • 41
2
votes
0 answers

When accessing another component's function in Angular, this keyword is not working

export class ComponeComponent implements OnInit { constructor(private cService: CService, private toastr: ToastrService) { } //class variable so that I can assign perticular client object clientObj: any; //to get all the client clients:…
Laharee
  • 21
  • 3
2
votes
3 answers

html container not found at createChild in amCharts Angular

In here when i am in the same page it loads the chart.But when i click another page and again come to this page chart is not loading and it shows like html container not found at createChild But if i again reload the same page chart will…
Hans
  • 308
  • 7
  • 20
2
votes
2 answers

How to assign value to interface property from http/async call in Angular?

I have a service that returns a JSON object and I want to assign this data to an interface property. Here is the following code, the component.ts code here has been stripped down to contain only the relevant parts. Service.ts file import {…
Controless
  • 23
  • 4
2
votes
2 answers

Angular: ngOnInit() called in every navigation

I work with Angular 9 and I use RouterModule. I read somewhere that ngOnInit() function called only one time between navigations, I mean every component initialized only once (ngOnInit() function). I found that is not true. Example of my…
matan___4
  • 73
  • 1
  • 7
2
votes
2 answers

ngOninit runs only once - form is not updated

I have material sidenav container with material navigation list:
PabloPhones
  • 123
  • 1
  • 1
  • 9
2
votes
3 answers

Angular 8 getRole is not a function in nested object

I have an API, that returns me the Profile info and I am fetching it in my HTTP service. The nested object User has a method getRole(). But when I am trying to call this method, I get ERROR TypeError: _co.profile.user.getRole is not a…
Tomas Lukac
  • 1,923
  • 2
  • 19
  • 37
2
votes
3 answers

Understanding ngOnInit

I am doing a udemy course on angular and came across this code. I have a sample project which has a Add item button which adds a new item in the array and displays the updated array on screen. shopping-edit.component.ts export class…
sanjeev u rao
  • 93
  • 1
  • 1
  • 4
2
votes
3 answers

I want to use the results of .subscribe in a method inside a second method in ngOnInit() { }

I have a method that uses a service to get a list of books, i needed a second method that uses the list of books to get one book by filtering it by id. The two methods are called in "ngOnInit" of the same component. What i get is in the second…
2
votes
0 answers

How to change route on angular ui-router without ngOnInit being called?

I have an angular 7 application using a angular ui-router@2.0.2 where I need to switch between views while maintaining the state of the views all the while. I dont want ngOnInit to be called upon each route change, I'd rather want the views (even…
2
votes
1 answer

Benefit of defining service at ngOnInit rather at Constructor in Angular

I am aware some of the facts where ngOnInit() and constructor differ. also I have come across many examples where services are defined at ngOnInit() method rather in a constructor and vice-versa. But using services at constructor level is not…
Jason
  • 325
  • 2
  • 4
  • 12
2
votes
2 answers

Why data initialization should not be done in the constructor of the component in Angular?

I am new to Angular 6. I have a requirement that I need to initialize all the drop-down fields through API calls. Many developers suggest that api calls for the data initialization should be done inside the ngOninit rather than doing it through…
Harry
  • 75
  • 8
2
votes
0 answers

Angular: function calling on constructor

I'm following this auth0 tutorial, and it says there: The authentication service's handleAuth() method must be called in the app.component.ts constructor so it will run on initialization of our app: // src/app/app.component.ts import { AuthService…
james keller
  • 99
  • 2
  • 7
1 2
3
15 16