Angular 4:ngComponentOutlet - 数据绑定

时间:2017-05-27 18:42:49

标签: angular data-binding

我正在尝试将数据绑定到Angular 4中的ng-container。 组件加载正常,但是当我添加[componentData] ="测试"我收到错误

<ng-container *ngComponentOutlet="components.name"  [componentData]="testing">
</ng-container>

错误

Error: Template parse errors:
Can't bind to 'componentData' since it isn't a known property of 'ng-container'.
1. If 'componentData' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.
2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("'Textarea' ">
                                      <ng-container *ngComponentOutlet="widget.widgetComponent.component"  [ERROR ->][componentData]="testing">
                                      </ng-container>
                            </div>

您是否可以将数据绑定到ngComponentOutlet?

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可以创建自定义组件

'custom-ng-container'

@Input() componentName : any;
@Input() componentData : any[];

HTML看起来像

<ng-container *ngComponentOutlet="componentName" >

</ng-container>

您应该在父组件中使用

<custom-ng-container [componentName]="components.name"  [componentData]="testing"> </custom-ng-container>