【问题标题】:Initialize Angular 2 component with attribute [duplicate]使用属性初始化Angular 2组件[重复]
【发布时间】:2016-05-11 06:50:04
【问题描述】:

有没有办法将参数传递到 Angular 2 组件的根目录?我已经能够使用属于内部组件模板的组件而不是根组件来做到这一点:

<my-component [test] = "some-val"></my-component>

在我的组件上:

export class AppComponent implements OnInit {
    test: string;

    ngOnInit() {
        console.log(this.test); //test is undefined
    }
}

这样做的原因是使用 Razor 变量传递给 Angular。例如编辑用户。我可以这样做:

<edit-form [model] = "@Model.Id" > </edit-form>

【问题讨论】:

    标签: angular


    【解决方案1】:

    Angular 不会从 index.html 仅从其他组件中进行任何绑定。

    在根组件中获取属性值的解决方法是

    export class AppComponent {
      constructor(elementRef:ElementRef) {
        console.log(elementRef.nativeElement.getAttribute('inputField));
      }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多