【问题标题】:Angular2 Injecting app variable from html page to app [duplicate]Angular2将应用程序变量从html页面注入应用程序[重复]
【发布时间】:2016-04-19 15:46:48
【问题描述】:

我想将变量从包含 html 页面传递到我的应用程序。

注意:我知道如何在组件之间传递,但问题来自应用程序之外!

示例 我的html页面:

  <body>
    <my-app customerId="123">Loading...</my-app>
  </body>

我希望能够在我的应用程序中使用“customerId”变量。

从 'angular2/core' 导入 {Component};

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { 
  customerId;
}

如何做到这一点?

我尝试输入但没有成功。

  @Input("customerId") customerId;

谢谢。

【问题讨论】:

    标签: angular


    【解决方案1】:

    根组件不支持@Input() 绑定。

    另见https://github.com/angular/angular/issues/1858

    作为一种解决方法,您可以使用

    constructor(public elementRef: ElementRef) {
        var native = this.elementRef.nativeElement;
        var myattr = native.getAttribute("myattr");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2017-04-10
      • 2021-04-10
      相关资源
      最近更新 更多