我现在正在尝试将数据绑定(bind)到 Angular 4 中的 ng-container。 组件加载正常,但是当我添加 [componentData]="testing"时出现错误
<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>
真的不能将数据绑定(bind)到 ngComponentOutlet 吗?
最佳答案
要解决这个问题,您可以创建一个自定义组件
'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>
关于 Angular 4 : ngComponentOutlet - data binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220466/