I apologize in advance for not creating a plunker for this code, there is a
number of third party controls that are needed for it to run.
My question is about selecting service, dependency injection, at run time,
based on some conditions.
lets discuss only one service for this example.
import { Injectable } from '@angular/core';
import { Http, Response} from '@angular/http';
@Injectable()
export class Service1 {
constructor (
private http: Http) {
}
}
In my component if I have a fixed one, all is well.
import {Component, ViewChild, OnChanges, Input, ReflectiveInjector,
SimpleChange} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import { Service1 } from './my.service.1';
@Component({
providers: [
Service1
],
moduleId: module.id,
selector: 'my-component',
templateUrl: './my-component.component.html'
})
export class MyComponent {
constructor(private service : Service1
) {
}
in the above case all is well, no runtime errors.
Now if I change to the dynamic version, I get an error
EXCEPTION: Error in http://localhost:3000/app/app.component.html:5:8 caused by:
No provider for Http! (Service1 -> Http)
the version of code causing the error is:
I added a member variable for MyComponent: private service: any = '';
and the constructor now looks like
constructor() {
// this does not work with the http module
let injector = ReflectiveInjector.resolveAndCreate([Service1]);
this.service = injector.get(Service1);
}
anyone sees what is causing the above mentioned error?
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.