Main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule} from '@angular/http';
import { AppComponent } from './app.component';
//service
import { DataService } from './shared/data.service';
@NgModule({
imports: [ BrowserModule, FormsModule, HttpModule],
declarations: [ AppComponent ],
providers: [ DataService],
bootstrap: [ AppComponent ]
})
export class AppModule {}
起点
<script>
var temp = "Prafulla";
System.import('app').catch(function (err) { console.error(err); });
</script>
我想要to use temp variable inside angular2
我尝试使用此链接Here但是没有工作我不知道如何以及在哪里编写主要功能my app.module.ts
怎么做?
答案 0 :(得分:4)
您必须在AppModule的提供程序中添加您的值:
@NgModule({
imports: [ BrowserModule, FormsModule, HttpModule],
declarations: [ AppComponent ],
providers: [
DataService,
{provide: 'rootVar', useValue: rootVar}
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
现在您应该能够访问组件和服务中的值:
export class MyComponent {
constructor(@Inject('rootVar') rootVar) {}
}