Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

Parent Component (MCQComponent)

import { Component, OnInit } from '@angular/core';  @Component({     selector: 'mcq-component',     template: `         <div *ngIf = 'isQuestionView'>             .....         </div>         <result-comp *ngIf = '!isQuestionView' [answers] = 'ansArray'><result-comp>     `,     styles: [         `             ....         `     ],     providers: [AppService],     directives: [SelectableDirective, ResultComponent] }) export class MCQComponent implements OnInit{       private ansArray:Array<any> = [];     ....     constructor(private appService: AppService){}     .... } 

Child Component (result-comp)

import { Component, OnInit, Input } from '@angular/core';  @Component({     selector:'result-comp',     template: `         <h2>Result page:</h2>      ` }) export class ResultComponent implements OnInit{     @Input('answers') ans:Array<any>;      ngOnInit(){         console.log('Ans array: '+this.ans);     } } 

When run, console.log is showing up two times, the first time it shows the correct array but the second time it gives undefined. I've not been able to figure it out: why is ngOnInit in ResultComponent getting called twice?

like image 928
Sagar Ganesh Avatar asked Aug 05 '16 11:08

Sagar Ganesh


People also ask

Why ngOnInit is called twice?

Right now, if an error happens during detecting changes of content/view children of a component, ngOnInit will be called twice (seen in DynamicChangeDetector). This can lead to follow up errors that hide the original error.

When ngOnInit is called?

ngOnInit is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

Is ngOnInit called first or constructor?

ngOnInit() is a better “starting point” – this is where / when component combinations are solved. We use constructor() for all the initialization/declaration. It's better to avoid writing actual work in the constructor. The constructor() should only be used to initialize class members but shouldn't do actual “work”.

What is the primary reason of using ngOnInit?

One of the most common use cases for ngOnInit , is to establish observable assignments to component fields. It's good practice to do this in the ngOnInit so that observables are initialized at a predictable time in the component lifecycle.


2 Answers

Why it is called twice

Right now, if an error happens during detecting changes of content/view children of a component, ngOnInit will be called twice (seen in DynamicChangeDetector). This can lead to follow up errors that hide the original error.

This information comes from this github issue


So it seems that your mistake might have an origin elsewhere in your code, related to this component.

like image 102
Dylan Meeus Avatar answered Sep 21 '22 03:09

Dylan Meeus


This was happening to me because of a faulty component html. I had forget to close the selector tag in the host component. So I had this <search><search>, instead of <search></search> - take note of the syntax error.

So related to @dylan answer, check your component html structure and that of its parent.

like image 42
parsethis Avatar answered Sep 20 '22 03:09

parsethis