简体   繁体   English

Angular 4:ngComponentOutlet - 数据绑定

[英]Angular 4: ngComponentOutlet - data binding

I am trying to bind data to an ng-container that is in Angular 4 now. 我正在尝试将数据绑定到Angular 4中的ng-container。 The component loads fine, but when I add [componentData]="testing" I get error 组件加载正常,但是当我添加[componentData] =“testing”时,我得到错误

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

Error 错误

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>

Is it true you can't bind data to ngComponentOutlet? 你不能将数据绑定到ngComponentOutlet是真的吗?

To fix this you can create a custom component 要解决此问题,您可以创建自定义组件

'custom-ng-container'

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

HTML will look like HTML看起来像

<ng-container *ngComponentOutlet="componentName" >

</ng-container>

You should be using in your parent component as 您应该在父组件中使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM