Is it possible to inject a service inside another service and vice versa at the same time?

前端 未结 3 1900
日久生厌
日久生厌 2021-01-18 06:28

I have a real scenario in a real project where I need 2 services to access the properties and/or methods of each other. I\'m not an Angular expert so is it possible?

3条回答
  •  抹茶落季
    2021-01-18 06:47

    AngularJS does not allow injection of circular dependencies.

    Miško Hevery, one of the authors of AngularJS, recommends finding the common elements:

    +---------+      +---------+
    |    A    |<-----|  B      |
    |         |      |  |  +-+ |
    |         |      |  +->|C| |
    |         |------+---->| | |
    |         |      |     +-+ |
    +---------+      +---------+
    

    And extracting it to a third service:

                             +---------+
    +---------+              |    B    |
    |    A    |<-------------|         |
    |         |              |         |
    |         |    +---+     |         |
    |         |--->| C |<----|         |
    |         |    +---+     +---------+
    +---------+
    

    For more information, see Circular Dependency in constructors and Dependency Injection by Miško Hevery.

提交回复
热议问题