2016-11-24 68 views
2

我需要一台服务器生成的字符串传递到客户端,使用类似PHP嵌入到HTML这样的:传递变量角度应用是全球性的

<html> 
    //head... 

    //body... 
    <script> 
     System.import("app") 
      .then(function(module) { 
       module.main({authToken: '<?php echo $randomString; ?>'}); 
      }) 
      .catch(console.error.bind(console)); 
    </script> 
    // end of body... 
</html> 

我得到了它在main.ts类似:

export function main(randomString: string) { 
    platformBrowserDynamic([{provide: 'randomString', useValue: randomString}]) 
     .bootstrapModule(AppModule); 
} 

和AppComponent喜欢:

constructor(randomString) { 
    console.log(this.randomString); 
} 

后它我

zone.js:355 Unhandled Promise rejection: Can't resolve all parameters for AppComponent: (?). ; Zone: <root> ; Task: Promise.then ; Value: Error: Can't resolve all parameters for AppComponent: (?).(…) Error: Can't resolve all parameters for AppComponent: (?). 

我该如何正确地将一个变量作为提供程序传递给引导程序,并在例如AppComponent或其他组件或服务中使用?

感谢

回答

2

要注入的是使用一个字符串键提供的值,你需要使用@Inject()装饰

constructor(@Inject('randomString') private randomString:string) { 
+1

OMG!不知道我在之前的版本中忘记了什么,但它的功能类似于魔术......至少也许我的问题将成为这条道路上下一个陌生人的完整指南。谢谢! – eatmypants