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

Angular passing data to variable and that variable usage

Declaring variable export class TestComponent implements OnInit { testVar:string; then initialising ngOnInit() { this.somefunction works.subscribe( this.testVar='testData'; console.log('log1 '+this.testVar); ); console.log('log2…
David
  • 4,332
  • 13
  • 54
  • 93
-1
votes
2 answers

Data not being populated in ngOnInit in Angular

I am calling a dummy web API to populate a variable of class to use it further but then I came across an odd scenario which is confusing me: export class myClass implements OnInit{ data : any; constructor (private http:HttpClient){} …
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
-1
votes
2 answers

How to loop through an Array in a subscribe and ngOnInit method?

I have a question i want to loop through an .subscribe() method which is in an ngOnInit() method: ngOnInit() { this.service.getEmployees().subscribe( (listBooks) => { this.books = listBooks var events: CalendarEvent[] =…
-3
votes
1 answer

What is the significance of ngOnInit() in Angular?

Can we not perform all the same operations it does in the constructor?
-5
votes
1 answer

Asynchronous ngOnInit

Is it allowed to have ngOnInit() return a promise? I see a lot of code written that way that seems to be working. However, a non-observed promise can be resolved/rejected at any time, for instance when component is not allowed to be modified.…
user3284063
  • 665
  • 5
  • 20
1 2 3
15
16