Questions tagged [angular2-di]
60 questions
133
votes
6 answers
Getting instance of service without constructor injection
I have a @Injectable service defined in Bootstrap. I want to get the instance of the service without using constructor injection. I tried using ReflectiveInjector.resolveAndCreate but that seem to create a new instance.
The reason I'm trying to do…

dstr
- 8,362
- 12
- 66
- 106
40
votes
2 answers
How do I inject a parent component into a child component?
I'm trying to inject a parent component into a child component. I thought this would be straightforward – simply specify/inject the parent component in the child's constructor():
constructor(private _parent:AppComponent) {} // child component…

Mark Rajcok
- 362,217
- 114
- 495
- 492
25
votes
5 answers
How to use Dependency Injection (DI) correctly in Angular2?
I have been trying to figure out how the (DI) Dependency Injection work in Angular2. I ran into lots of problem/issue every time when I tried to Inject a service/or class into my components.
From different googled articles, I need to either use…

George Huang
- 2,504
- 5
- 25
- 47
18
votes
3 answers
Access a service from a custom validator in Angular2
I need to access my custom http service from inside a static method, as example:
import {Control} from 'angular2/common';
import {HttpService} from './http.service';
class UsernameValidator {
static usernameExist(control: Control):…

Silencer
- 1,602
- 4
- 17
- 27
18
votes
4 answers
How to add providers to Injector dynamically?
Each component can specify new Providers using its providers property in ComponentMetadata.
Is there a way to specify providers dynamically from, say, constructor of the component?

Kugel
- 19,354
- 16
- 71
- 103
16
votes
3 answers
Angular 2 Di not working - cannot resolve all parameters for
I've created a simple Hello World app that works fine. But when I want to add a "Service" just a simple Di I got the following errors:
angular2.dev.js:23877 EXCEPTION: Cannot resolve all parameters for 'AppComponent'(?). Make sure that all the…

squadwuschel
- 3,328
- 3
- 34
- 45
15
votes
2 answers
Angular2 passing function as component input is not working
I've a component that takes function as input. I've passed this function from parent.
Though the function is called, the function is not able to access the dependencies of the instance on which this function is declared.
Here is the…

Ashok Koyi
- 5,327
- 8
- 41
- 50
12
votes
1 answer
Why use `deps` property in DI
Here is the code snippet from angular.io:
{ provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService] }
...
export function runnersUpFactory(take: number) {
return (winner: Hero, heroService: HeroService): string => {
…

Max Koretskyi
- 101,079
- 60
- 333
- 488
8
votes
2 answers
Angular 2 get service by string name
Is there a way get an instance of an injectable service of angular 2 just by the service name?
For Example, in Angular 1 you could write:
var service = $injector.get('ServiceName');
and the service variable would get an instance of the service.
I'd…

Ron Avraham
- 478
- 2
- 7
- 18
8
votes
4 answers
Angular 2 - Singleton Services?
In Angular 1 I frequently used factories for services to store shared state accessible by many components. It looks like in Angular 2 all services that are injected as @Injectable() are created each time, thus losing the shared state.
I 'register'…

Rick Strahl
- 17,302
- 14
- 89
- 134
6
votes
0 answers
Angular2 DI: How to inject dependency under @NgModule?
TL;DR 25.10.16: update
I renamed the title to provide more clarification as i am still concerned about the right solution.
Given a module
@NgModule({
imports: [
ExternalModule.forRoot(config),
],
providers:…

ford04
- 66,267
- 20
- 199
- 171
5
votes
1 answer
Angular. Router DI not working when using APP_INITIALIZER
I'm preloading app configuration from server with APP_INITIALIZER in following way, AppModule:
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: configServiceFactory,
deps: [ConfigService],
multi:…

Alex Kalmikov
- 1,865
- 19
- 20
5
votes
1 answer
How do you inject an angular2 service into a unit test? (RC3)
I am using RC3. I am implementing the new Angular2 router as documented here: https://angular.io/docs/ts/latest/guide/router.html
Everything works fine but I am having problem in unit testing. Specifically, I cannot inject Angular2 services into my…

danday74
- 52,471
- 49
- 232
- 283
4
votes
1 answer
Unit testing a parent class that uses Injector with Router (and other services)
Suppose I have class Foo which provides dependency injected services to all its derived classes, as such:
export class Foo {
protected fb : FormBuilder;
protected router : Router;
protected otherService : OtherService;
protected…

prettyfly
- 2,962
- 1
- 25
- 27
4
votes
1 answer
Does lazy module create child injector of a root injector
Suppose I have the following LazyModule that is lazily loaded and the LazyComponent declared inside of it:
@NgModule({
declarations: [LazyComponent],
providers: [LazyModuleService],
})
export class LazyModule { ...
@Component({
…

Max Koretskyi
- 101,079
- 60
- 333
- 488