简体   繁体   English

在 Angular 5 中,如何从父组件访问动态添加的子组件?

[英]In Angular 5, how to get access to dynamically added child components from parent component?

I am developing an Ionic + Angular 5 project.我正在开发一个 Ionic + Angular 5 项目。

I need to dynamically load some components.我需要动态加载一些组件。 Following the Dynamic Component Loader Example in the Angular documentation I am able to successfully load components and displayed them.按照 Angular 文档中的动态组件加载器示例,我能够成功加载组件并显示它们。

I have a need, however, to be able to loop through the loaded components.但是,我需要能够遍历加载的组件。

I can easily maintain my own list of components in the parent as I create and destroy the child components but it seems to me that that's ugly and not the "Angular Way".在创建和销毁子组件时,我可以轻松地在父组件中维护自己的组件列表,但在我看来,这很丑陋,而不是“角度方式”。

After much experimentation and research it seems that I cannot use @ViewChildren to access dynamically added components since they have their own views.经过大量实验和研究,似乎我无法使用 @ViewChildren 访问动态添加的组件,因为它们有自己的视图。 ViewChildren not finding Dynamically components ViewChildren 未找到动态组件

Is this correct?这样对吗?

I am able to use viewContainerRef.get() to iterate over the child views and retrieve ViewRefs.我可以使用 viewContainerRef.get() 遍历子视图并检索 ViewRefs。 But there doesn't seem to be a way to get a reference to the component given a ViewRef?但是似乎没有办法在给定 ViewRef 的情况下获得对组件的引用?

I notice the 'rootNodes' property of the ViewRef however that property is not documented in the Angular docs View Ref However, it is documented in the EmbeddedViewRef docs Embedded View Ref but that still doesn't seem to give me access to the component.我注意到 ViewRef 的“rootNodes”属性,但是该属性没有记录在 Angular 文档视图参考中。但是,它记录在 EmbeddedViewRef 文档嵌入式视图参考中,但似乎仍然没有让我访问组件。

So the question is when I have a parent that dynamically adds child components how can I from the parent loop through the child components (so I can call one of my methods in the child)?所以问题是当我有一个动态添加子组件的父级时,我如何从父级循环到子级组件(这样我就可以在子级中调用我的方法之一)? My suspicion is that I am missing something stupid simple, have some fundamental misconception, or that there is a different pattern that I should be using.我怀疑是我遗漏了一些愚蠢的简单东西,有一些根本的误解,或者我应该使用不同的模式。

I've modified the Dynamic Component Loader example to highlight what I am trying to do.我修改了 Dynamic Component Loader 示例以突出显示我正在尝试做的事情。 Modified Dynamic Component Loader修改后的动态组件加载器

So, I thought about this and a clean way to do this (in my opinion), is to pass create a property on the parent component, such as parentContext and to pass the data or method into this object.所以,我想到了这一点,一个干净的方法(在我看来)是在父组件上传递 create 一个属性,例如parentContext并将数据或方法传递给这个对象。

Example - in parent component, you can even pass a method into your child component and it will run within the context of the parent.示例 - 在父组件中,您甚至可以将方法传递给子组件,它会在父组件的上下文中运行。 :

  gotoDetail(hero: Hero): void {
    this.entry.clear();
    const factory = this.resolver.resolveComponentFactory(DynamicModalChildComponent);
    this.dynamicModalComponentRef = this.entry.createComponent(factory);
    this.dynamicModalComponentRef.instance.hero = hero;
    // Pass parent context to child in order to close modal hierarchically (parent/child component relationship).
    // This approach is less verbose and makes it easier to unit test than a service in my opinion.
    this.dynamicModalComponentRef.instance.parentContext = () => this.destroyComponent();
}

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

相关问题 如何动态添加父组件时,如何访问父组件的子组件值? - How can I access child components values from a parent component when they are added dynamically? 如何从 Angular 9 中的子组件访问父组件 - How to Access Parent Component from Child Component in Angular 9 如何从 Angular 中的父组件访问子组件? - How to access child component from parent component in Angular? 如何从 Angular 中的子路由组件访问父组件属性? - How to access parent component properties from a child route component in Angular? React:如何动态地将不同类型的子组件附加到父组件中? - React: How to dynamically append child components of differing types into a parent component? 如何从父表单组件获取数据到子组件嵌套组件数据从父到子 - How to get data from parent form component to child component nested components data from parents to children 如何在父组件中使用子组件http响应? 角度的 - how to use child components http response in parent component? Angular 如何在 Angular 中将组件从父组件复制并传递给子组件? - how can i copy and pass components from a parent to a child component in Angular? 来自子组件中父组件的Angular(1.5)JS访问变量? - Angular (1.5) JS Access Variable from Parent Component in Child Component? 在Angular的父组件中创建子组件 - Create child components in the parent component in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM