arunselvakumar / AngularNotes

0 stars 0 forks source link

Angular Providers #3

Open arunselvakumar opened 5 years ago

arunselvakumar commented 5 years ago

Providers provided in the module level are singleton Providers provided in the component level are created for every instances of the component.

arunselvakumar commented 5 years ago

TestBed.get(ToDoService) -> Get the instance, only if the provider is registered in the application level. ie., at the module level.

arunselvakumar commented 5 years ago

To get the providers, which are registered in the component level - We need to use the following code.

fixture.debugElement.injector.get(ToDoService);

arunselvakumar commented 5 years ago

.and.returnValue(Observable.from([[1,2,3]]));

arunselvakumar commented 5 years ago

fixture.detectChanges() => Calls ngOnInit, so the dependency should be injected before that.