简体   繁体   中英

does angular cache dependencies via constructor injection?

I have a very strange situation.

I have a service class called Test1 which has an instance variable public account = new Subject(); Then this class is injected into 2 different components , but those component are parent-child . Now what happens is first's component's code changes something into that instance variable( Test1's account ). It turns out as soon as this change happens, another component is subscribing to it and when changed happened, subscribe happened which leads me to the following question.

Question 1)WHen service class got injected into 2 components, let's say it got injected into compA first, and then I think in compB, it got injected from cache and both components have same class.

QUestion 2) does angular cache dependencies when injecting into constructor?

Question 3) whose job is to to cache dependencies ? is it Provider's object we use in modules?

  1. Both components will have same Instance depending on which level a service is provided. You can provide services in both modules and components. If it is provided in a module in which both the components reside and no where else, then both will have same instance. If it is just provided in parent component, then again both will have same instance. If it is provided in both the componnets then there will be different instances.
  2. No, It does it when we provide. (Using the term cache doesn't seem right to me but yeah..)
  3. Yes, when we provide, thats when angular will create singletons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM