angular: ngOnInit should never be called twice, even if an error happens

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.

/cc @matanlurey

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 44
  • Comments: 32 (4 by maintainers)

Most upvoted comments

Any updates on this issue? I’m also having the same issue in my project.

Hi,

Well the Problem in my case was the way I was bootstrapping the Child Components. In my @NgModule decorator’s metadata object ,I was passing ‘child component’ in the bootstap property along with ‘parent component’. Passing the child components in bootstap property was resetting my child components properties and making OnInit() fired twice.

@NgModule({ imports: [ BrowserModule,FormsModule ], // to use two-way data binding ‘FormsModule’ declarations: [ parentComponent,Child1,Child2], //all components
//bootstrap: [parentComponent,Child1,Child2] // will lead to errors in binding Inputs in Child components
bootstrap: [parentComponent] //use parent components only
})

Still reproducible with 2.0.2

Yep, still happens, ngOnInit and constructor fired twice just a per website is starting. Very disappointing, as it seems not possible to avoid it

2019 and still same issue. Constructor and NgOnInit called twice

The problem is that we cannot grab the original error. If someone could give a good advice how to catch the original error it would be great.

@francosioquim @prabhatsharma @DILEEPKUMAREBIX We really need a plunker that shows it. Otherwise it is super hard to reproduce your problem…

You can avoid such thing by enabling PROD mode

On Tue, Sep 17, 2019 at 2:56 PM dalvarezsmiet notifications@github.com wrote:

Yep, still happens, ngOnInit and constructor fired twice just a per website is starting. Very disappointing, as it seems not possible to avoid it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular/issues/6782?email_source=notifications&email_token=AD3Q3VRBFBX6CRRBXJCDEETQKCPEDA5CNFSM4BZ6YCCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD634UOA#issuecomment-532138552, or mute the thread https://github.com/notifications/unsubscribe-auth/AD3Q3VTQ2PU5ENVBASTHRTTQKCPEDANCNFSM4BZ6YCCA .

I am also facing the same issue, the constructor is getting called twice. I have created a thread here. does anyone have any workaround for this?

@francosioquim for me inputs were undefined not null (on the retriggered onInit)

I get the same issue. I tried to pass in the object from one component to another using @Input decorator, and using the parameter i build another object in the passed component in ngOninit lifecycle method. In the first iteration i was able to get the value but in the second iteration (which shouldn’t happen) reinitialized the values back to null.