Questions tagged [angular2-injection]

45 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
31
votes
3 answers

How to call component method from service? (angular2)

I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with this component. How to call component method from service? @Component({ …
26
votes
6 answers

Testing - Can't resolve all parameters for (ClassName)

Context I created an ApiService class to be able to handle our custom API queries, while using our own serializer + other features. ApiService's constructor signature is: constructor(metaManager: MetaManager, connector: ApiConnectorService,…
26
votes
4 answers

Angular 2 HTTP "Cannot resolve all parameters for 'AppService'"

I tried to import the http provider into a service, but I'm getting the following error: Cannot resolve all parameters for 'AppService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that…
12
votes
2 answers

ng2 - dynamically creating a component based on a template

I've been looking at the Angular 2 APIs for ComponentResolver and DynamicComponentResolver for creating dynamic components, but I have something different in mind than those APIs offer. Is there any way in NG2 to create a component based on a string…
parliament
  • 21,544
  • 38
  • 148
  • 238
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
7
votes
2 answers

Angular 2 DI: pass input binding to deps of factory provider

Is there an easy way to inject an input binding into the deps array of a provider factory? Below obviously does not work. const myServiceFactory = (object: any) => { //... }; @Component({ // ... inputs: ['object'], providers: [ …
Dominic
  • 183
  • 2
  • 8
6
votes
1 answer

What is Angular's Custom Module lifecycle?

Is there information about a Module's lifecycle. Currently the lifecycle of Components inside Angular is well documented and we have hook methods like ngOnInit(), ngDoCheck(), etc. Do we have similar lifecycle hooks for Angular Modules? Where can I…
Hivaga
  • 3,738
  • 4
  • 23
  • 36
5
votes
0 answers

Angular 2 must use @Inject(forwardRef(...)) for Service to be injected

I have an issue with one service within my application. The issue is that wherever this service is being injected I'm getting the following error: Uncaught (in promise): Error: Can't resolve all parameters for TestComponent: ([object Object], ?,…
Daniel Grima
  • 2,765
  • 7
  • 34
  • 58
5
votes
1 answer

Getting parent Component via Injector.get() when token can be multiple values

What I'm trying to do: Multiple different components that make use of a single directive when the directive is called, I need to be able to get the parent/host component that the directive is called from. Plnkr ->…
Edward
  • 1,076
  • 1
  • 12
  • 24
5
votes
3 answers

angular 2 - Injected service in http error handler

I have a method handleError() like in the documentation https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling private handleError(error: any) { console.error(error); console.log(this.loginService); // <- always…
Philipp
  • 4,180
  • 7
  • 27
  • 41
5
votes
2 answers

Angular 2 call service only on app initialization

What I am trying to accomplish is to call external API only once per app initialization. I have a simple service, @Injectable() export class XService { url = "http://api.example.com" constructor(private _http:Http) { } …
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
4
votes
2 answers

Angular 5 and lerna NullInjectorError: No provider for InjectionToken

My use case is the following: I have 2 angular5 libraries. Both of the libraries are cloned from the following project: https://github.com/robisim74/angular-library-starter I have a monorepo managed by lerna the child library has a very simple…
Yariv Katz
  • 1,353
  • 1
  • 17
  • 24
4
votes
3 answers

Angular2: Why to use private access specifier to instantiate the providers in constructor

Why to use private access specifier only to instantiate the providers in constructor ? Is there any specific reason to use private access specifiers only ? Private injection constructor(private service: InjectedService) Public…
Rajni
  • 95
  • 1
  • 8
4
votes
2 answers

Angular 2 service not being injected into component

I have a service defined in my Angular2 (2.0.0-beta.0) application. It's something like this: import {Injectable} from "angular2/core"; @Injectable() export class MyService { constructor() { } getSomething() { return…
Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
1
2 3