Reputation: 7417
I have this component using a value provider:
import { Component, OnInit, Injector } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [{
provide: "currency",
useValue : "dollar"
}]
})
export class AppComponent implements OnInit {
ngOnInit(): void {
}
title = "app works";
constructor(private injector: Injector){
this.title = "Currency is: "+ injector.get("currency");
}
}
When I run the program, the injector throws an error saying, "Cannot read property 'instance' of undefined
"
If it matters, my view is a simple <h1>{{title}}</h1>
.
What I'm I doing wrong?
Upvotes: 3
Views: 9784
Reputation: 3062
Don't use Injector, just import the 'Inject' decorator and use like so-
import { Component, OnInit, Inject } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [{
provide: "currency",
useValue : "dollar"
}]
})
export class AppComponent implements OnInit {
ngOnInit(): void {
}
title = "app works";
constructor(@Inject("currency") private currency){
this.title = "Currency is: "+ currency;
}
}
Upvotes: 2