I created a Message class like this
import { ReflectiveInjector } from '@angular/core';
import { ApiService } from '../api.service';
export class Message {
timestamp: number;
message: any;
api: ApiService;
constructor(message: any) {
let injector = ReflectiveInjector.resolveAndCreate([ApiService]);
this.api = injector.get(ApiService);
this.timestamp = message.timestamp;
this.message = message.message;
}
}
I am not entering ApiService directly into the constructor parameters, because I try to avoid this:
let nm = new Message(message, this.api)
I do not want the service to be in the parameters.
So, I am using ReflectiveInjector, but this code does not even work. I get this error: EXCEPTION: Error: Unprepared (in promise): There is no provider for Http! (ApiService → Http) , even if I turned on HTTP_PROVIDERS in this way
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { AppComponent, environment } from './app/';
import { appRouterProviders } from './app/app.routes';
if (environment.production) {
enableProdMode();
}
bootstrap(AppComponent, [
appRouterProviders,
HTTP_PROVIDERS,
])
.catch(err => console.log(err));
How can I use the constructor to instantiate the class and implement my services:
let nm = new Message(message);
thank
source
share