Angular 动态注入角度,将组件注入父组件

Angular 动态注入角度,将组件注入父组件,angular,Angular,如何在angular 4中将一个组件动态注入另一个组件,以及如何将值从父组件传递给子组件。任何人都可以帮助你编写示例工作代码。这个问题有点不清楚,而且肯定会因为没有任何示例代码来说明你尝试过的内容,以及缺乏关于你尝试完成的内容的详细信息而被否决 基本上,可以使用选择器作为HTML元素将组件包含在另一个组件中,如下所示: 在父组件中: <div> <ai-star [rating]='product.starRating' (ratingClicke

如何在angular 4中将一个组件动态注入另一个组件,以及如何将值从父组件传递给子组件。任何人都可以帮助你编写示例工作代码。

这个问题有点不清楚,而且肯定会因为没有任何示例代码来说明你尝试过的内容,以及缺乏关于你尝试完成的内容的详细信息而被否决

基本上,可以使用选择器作为HTML元素将组件包含在另一个组件中,如下所示:

在父组件中:

<div>
   <ai-star [rating]='product.starRating'
            (ratingClicked)='onRatingClicked($event)'>
   </ai-star>
</div>
@Component({
    selector: 'ai-star',
    templateUrl: 'app/shared/star.component.html',
    styleUrls: ['app/shared/star.component.css']
})
export class StarComponent implements OnChanges { ... }

您可以在这里找到完整的示例代码:

组件的动态注入请查看代码我们必须使用组件工厂分解器

let adItem = this.user;
    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(adItem.component);
    let viewContainerRef = this.injectHost.viewContainerRef;
    viewContainerRef.clear();
    let componentRef = viewContainerRef.createComponent(componentFactory);
    (<InjectComponent>componentRef.instance).data = adItem.data;
让adItem=this.user;
让componentFactory=this.componentFactoryResolver.resolveComponentFactory(adItem.component);
让viewContainerRef=this.injectHost.viewContainerRef;
viewContainerRef.clear();
让componentRef=viewContainerRef.createComponent(componentFactory);
(componentRef.instance).data=adItem.data;