简体   繁体   中英

how to pass input parameters to an angular 2 component from index.html file

I need to set input, configuration parameters, to a component that is not mentioned in index.html.

I can see how to do that for component selectors that are included in index.html.

How can I do from index.html?

You can have your configuration in your Main module class as below

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>{{name}} Sample</h2>
      {{myConfiguration | json}}
    </div>
  `,
})
export class App {
  name:string;
  myConfiguration:any=myConfiguration;
  constructor() {
    console.log(myConfiguration);
    this.name = 'Global Configuration'
  }
}

@NgModule({ 
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App]
})
export class AppModule {
    var myConfiguration={id:1,devmode:true};  

}

LIVE DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM