2

Every tutorial about DI in Angular 2 is to set the dependencies into the constructor. But what if I want to create an instance of the class and the class have some dependencies to other classes.

I have class A and B. Class B should be inject into A. But A is different every time and should be able to create a instance of it.

If I set up the DI in the constructor from A, how to call new A() ?

I tried to add B as private variable to A with the @Inject(B) decoration.

class A {
  @Inject(B) b: B;
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
R3Tech
  • 743
  • 2
  • 9
  • 22
  • If you're using DI, you *don't* call `new A()`; the point of it is you don't need to resolve the dependencies and create the instance yourself. – jonrsharpe Feb 25 '17 at 16:10
  • 1
    But A should be every time different, so I have to create a new instance of. – R3Tech Feb 25 '17 at 16:11
  • I don't know what you're trying to say. – jonrsharpe Feb 25 '17 at 16:11
  • A have foo as member. But foo is not the same. So I want to create instance of A, that I can set foo. If A provide by DI, foo will be everytime the same. – R3Tech Feb 25 '17 at 16:14
  • I don't see foo in your example. Please provide a specific demonstration of what you're trying to do, and why. – jonrsharpe Feb 25 '17 at 16:22
  • I dont understand what you dont understand. I have two classes A and B. A should be able to create single instances but should have use B as DI. Like B is a Singleton and I use in A `B.getInstance()` – R3Tech Feb 25 '17 at 17:43
  • Possible duplicate of [Angular 2 Inject Dependency outside Constructor](https://stackoverflow.com/questions/39101865/angular-2-inject-dependency-outside-constructor) – Nick Gallimore Aug 23 '19 at 15:33

1 Answers1

1

Angular dependency injection only supports constructor injection.

You can inject an injector

constructor(private injector:Injector) {}

foo() {
  var x = injector.get(B);
  var a = new A(b);
}

This might also help in your case where DI injects a factory function that returns a new instance every time it's called. Create new instance of class that has dependencies, not understanding factory provider

You can also set up new injectors, also one that include parent injectors for finding providers. See also Getting dependency from Injector manually inside a directive

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • thanks for the fast response. The both ways behind the links looks a bit ugly - more like "damn we need a workaround". I idea was to build a HTTP-Builder where I can build my request url and then only call request() on the builder which use http.get(....) – R3Tech Feb 25 '17 at 16:11
  • No idea why you find them ugly. I find them both quite straight forward. – Günter Zöchbauer Feb 25 '17 at 16:13
  • In the example above, you can also inject `B` into the constructor instead of the `Injector`. Subsequent calls to `injector.get(B)` will return the same instance (just in case this is unclear). – Günter Zöchbauer Feb 25 '17 at 16:14
  • I find the DI in constructors particularly bad with SubClasses that need to DI and `super(the_DI_item)` if the super class needs them. If you just DI `Injector` and pass to the super then it removes the need to constantly update all constructors. Thanks. – HankCa Apr 27 '17 at 02:09
  • But IMHO it makes the code harder to reason about. I don't need to update contructors often, but I'm using Dart and here inheritance for components didn't land ye ;-). – Günter Zöchbauer Apr 27 '17 at 03:00