Hi Prashant,

TL:DR;
If you have something happening in your component, and you want to 
influence something up the hierarchy tree, you set this value in a service 
injected to both child and parent, and then move the parent's code in 
question from ngOnInit to a method that fires (or not) depending on this 
observable, or set an Output on the child that can then inform the parent 
that it wants (or wants not) the parent's action to be done.

On Tuesday, October 23, 2018 at 5:36:44 PM UTC+2, Prashant Barwe wrote:
>
>  How to prevent parent ngOnInit()
>

This is not how it works. In Angular's component tree, the hierarchy goes 
in the opposite direction: from parent to child. Child component can't 
control the parent - at least not in this phase.

So you might need to rephrase your question into "how to prevent 
ngOnInit()". Then you'd come to the second problem - you can't. This is a 
part of Angular Component lifecycle, and ngOn* methods always get called.

Maybe rephrase the question again then: "how to control the flow of 
component initialization?" E.g. during OnInit lifecycle, how do I make sure 
that some code gets executed or not, depending on outside context? Well, 
that's easy. Either you inject the outside contect via constructor (e.g. 
inject a service which knows whether an action is required or not) or via 
an @Input() binding, which is also (sometimes*) present for the ngOnInit 
lifecycle - meaning, it's parent component. Which would be, in your 
original scenario, your components' grandparent. So we're back to start, 
meaning again injecting a service or shooting an event via @Output().


-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to