-4

I know that we use @Input decorator to pass the values from parent component to the child component.

But, I am wondering instead of doing this, if we can create an instance of the Parent class in the constructor(Dependency Injection) and then get the variable value as instance.variable. Will it not be the same?

Help would be much appreciated.

  • you CAN do this, but you should not, and it's not the same for many reasons. the most obvious of which is that injecting the parent to the child would only work with ONE parent class, resulting in extremely tight coupling between parent and child, whereas you can use an input with ANY parent class, resulting in very loose coupling. There are also other more technical reasons it's a bad idea to be injecting the parent vs using an input. – bryan60 Aug 29 '20 at 23:19
  • Oh I see. Thanks for clarifying. – K.Sudha Snigdha Aug 30 '20 at 04:05

1 Answers1

1

You are just describing services. Components gets initiated and you can access via decorator @ViewChild but components have more up their sleeves and not practical to use for that purpose. You can use Services and initiate at any level you need, module level or component level.