1

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 {}

starting point

  <script>
    var temp = "Prafulla";
    System.import('app').catch(function (err) { console.error(err); });
 </script>  

I want to use temp variable inside angular2 I tried using this link Here but not work I donot understand how and where to write main function my app.module.ts

how to do it?

7
  • What is error you are getting? Commented Jan 30, 2017 at 10:09
  • TypeError: AppModule.main is not a function Commented Jan 30, 2017 at 10:10
  • You have to add the provider in the app.module.ts Commented Jan 30, 2017 at 10:10
  • System.import('app').then(function (AppModule) { AppModule.main(temp); }).catch(function (err) { console.error(err); }); Commented Jan 30, 2017 at 10:12
  • can you give me hint because I tried like creating main method in app.module.ts but not work Commented Jan 30, 2017 at 10:13

1 Answer 1

3

You have to add your value in the providers of the AppModule:

@NgModule({
   imports: [ BrowserModule, FormsModule, HttpModule],
   declarations: [ AppComponent ],                
   providers: [
                 DataService,
                 {provide: 'rootVar', useValue: rootVar}
              ], 
   bootstrap: [ AppComponent ]
  })
 export class AppModule {}

Now you should be able to access the value in your components and services:

export class MyComponent {
    constructor(@Inject('rootVar') rootVar) {}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.