我有组件’Parent’和’Child’。从Parent我们可以添加或删除子。所以孩子们很有活力。在Parent中我已经在循环中呈现了Child组件,如下所示
Parent.component.html
<孩子* …
你可能想用 @ViewChildren
在您的父组件中:
@ViewChildren(ChildComponent) children: QueryList<ChildComponent>; areChildrenValid(): boolean { const invalid = this.children.some(c => !c.IsValid()); return !invalid; }
注意 children 将在之后定义 AfterViewInit 钩。
children
AfterViewInit
@angular/core 提供 ViewChildren 和 QueryList ,这可能对你有帮助。
@angular/core
ViewChildren
QueryList
<child #varName></child> import { ViewChildren, QueryList } from '@angular/core'; @ViewChildren("varName") customComponentChildren: QueryList<YourComponent>; this.customComponentChildren.forEach((child) => { let retult = child.IsValid(); })
您可以使用组件选择器:
@ViewChildren(ChildComponent) childrenList: QueryList<ChildComponent>;
并通过它循环并确定有效性。