typescript - Angular 2: Inject service into class -
i have angular class represents shape. want able instantiate multiple instances of class using constructor.
the constructor takes multiple arguments representing properties of shape.
constructor(public center: point, public radius: number, fillcolor: string, fillopacity: number, strokecolor: string, strokeopacity: number, zindex: number)
inside class want use service provides capability draw shapes on map. possible inject service class , still use constructor standard way.
so want below , have angular automatically resolve injected dependency.
constructor(public center: geopoint, public radius: number, fillcolor: string, fillopacity: number, strokecolor: string, strokeopacity: number, zindex: number, @inject(drawingservice) drawingservice: drawingservice)
i've managed resolve problem.
angular 2 provides reflective injector allows inject dependencies outside of constructor parameters.
all had import reflective injector @angular/core
.
import {reflectiveinjector} '@angular/core';
and then:
let injector = reflectiveinjector.resolveandcreate([drawingservice]); this.drawingapi = injector.get(drawingservice);
the class doesn't have decorated @injectable
decorator. problem have provide dependencies drawingservice , nested dependencies, hard maintain.
Comments
Post a Comment