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?
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.
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.
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”.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With