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;
}
7
  • 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. Commented Feb 25, 2017 at 16:10
  • 1
    But A should be every time different, so I have to create a new instance of. Commented Feb 25, 2017 at 16:11
  • I don't know what you're trying to say. Commented Feb 25, 2017 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. Commented Feb 25, 2017 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. Commented Feb 25, 2017 at 16:22

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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(....)
No idea why you find them ugly. I find them both quite straight forward.
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).
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.
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 ;-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.