I follow the angular docs for Injection Dependency Injection and tried to duplicate the injection tokens section . But it is clear that I still do not understand this.
I am trying to use value provider
for input config:any
in my project.
At the simplest level, I just want the provide
string const
const CFG_STRING = "I was declared externally and injected in ngModule"
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
providers: [
{provide: CFG_STRING, useValue: CFG_STRING}
],
bootstrap: [ App ]
})
and inject
into the component constructor
const LOCAL_STRING = "I was declared in the local module"
export class App {
constructor(
// @Optional() cfgString: CFG_STRING
) {
this.name = 'Angular2'
this.local = LOCAL_STRING
}
}
But when I do this, the application does not work correctly. What am I doing wrong?
Here plunkr: http://plnkr.co/edit/sQwxyDqLkMTgVUjJ1yYF?p=preview
source
share