Can't get injector in angular 2

316 Views Asked by At

I have DrawingDataService that contains array of my data and different tools to draw this data. I want to use DrawingDataService like singleton in all of my tools.

I set DrawingDataService in AppModule providers list (to make as it singleton instance):

@NgModule({
declarations: [
    AppComponent
],
imports: [
    BrowserModule,
    FormsModule,
    HttpModule
],
providers: [
    DrawingDataService
],
bootstrap: [AppComponent]
})
export class AppModule { }

Then I add injector in my tool:

export class LineTool extends BaseTool {

constructor(private injector: Injector) {

    super();

    let drawingDataService2 = this.injector.get(DrawingDataService);

And it says me that this.injector is null. Is it a good practice to make singleton service for storing data?

1

There are 1 best solutions below

0
On

Try to do this in next way:

constuctor( private drawingDataService: DrawingDataService)

And you can use it in next way:

this.drawingDataService.someMethod()