与ComponentFactoryResolver绑定的Angular2属性

时间:2016-11-24 15:50:23

标签: angular property-binding

我正在尝试在一个HTML页面中引导多个angular2组件。

  <body>
    <cart id="cart">Loading CartComponent content here ...</cart>
    <p>The text between components</p>
    <item [itemId]="1" [itemName]="Cool Item1" id="item-1">Loading ItemComponent id=1 content here ...</item>
    <p>The text between components</p>
    <item [itemId]="2" [itemName]="Cool Item2" id="item-2">Loading ItemComponent id=2 content here ...</item>
    <p>The text between components</p>
    <item [itemId]="3" [itemName]="Cool Item3" id="item-3">Loading ItemComponent id=3 content here ...</item>

  </body>

到目前为止,除了在index.html中从服务器传递的属性的属性绑定外,一切正常。

以下是plunker https://plnkr.co/edit/5DbVda?p=preview

1 个答案:

答案 0 :(得分:2)

Angular2中通常不支持绑定根元素标记。

您可以使用

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

另见Initialize Angular 2 component with attribute

相关问题