Angular get status of all children components from parent component

Rajiv

I have components 'Parent' and 'Child'.From Parent we can add or remove child. so children are dynamic. in Parent I have rendered Child component in loop like below

Parent.component.html

<child *ngFor="let child of children" [data]="child"></child>

Now in child component I have added a function called IsValid() to check child is valid or not

Child.component.ts

IsValid()
{
  //check validity of component and return true if valid else false
}

in parent component I have a button called 'Save' I have to enable that button if all child's are valid else need to disable that button.

So I need a way to call Child components IsValid function for each child component from Parent and then determine the validity result and apply it to Save button to enable or disable

What I have tried

1. I have emited valid or invalid result from child to parent and if any childs result is invalid i have disabled save button.

but problem here is : if I have added one child, make it valid, save button will be enabled. now I have added another child which is invalid so save button will be disabled but if I remove invalid child save button will be disabled though we have only one child which is valid.. since IsValid event get emmited only if current child get change.

2. I can use something like this

<child #varName></child>

@ViewChild('varName') childElement;

and then from parent I can call

childElement.IsValid() 

but since I have rendered childrens in loop how to give unique name in loop and how to add reference to that unique HTML tag in ts file.

I have created case here SlackBlitz

Any help will be appreciated. Thanks

manish

@angular/core provides ViewChildren and QueryList, probably that should help you.

<child #varName></child>
import { ViewChildren, QueryList } from '@angular/core';
@ViewChildren("varName") customComponentChildren: QueryList<YourComponent>;

this.customComponentChildren.forEach((child) => { let retult = child.IsValid(); })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Send information from multiple children components to one parent component in React

Get consolidated data from all the child components in the form of an object inside a parent component : React JS

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

How do I get the offsetHeight of an element in a parent component from the children?

Svelte - pass variable from parent component to all its child components

React: Get state of children component component in parent

Get status of nested children in Vue components

Styling child components template from parent component css angular

Angular - emit event from parent component to dynamic child components

How to share the data from Parent Component to Multiple Child components in Angular?

Get all children from parent child JSON data

Get all the children inputs with a specific class starting from the parent form

Get all children and grandchildren from either of the two parent columns

Get all children models from a parent class - Django

VBA Excel get a value from parent to all it's children

Get data to Parent component from multi child components

Passing data from children to parent with n children in Angular without emiting in each component along the way

Prevent child's state from reset after parent component state changes also get the values of all child components:ReactJS+ Typescript

How to pass all children elements of parent component?

Angular 7 - Get component instance of all components declared in HTML

pass the status from the parent screen to the children screen

Angular 4 Parent-Children component interaction

React Functional Components change state of parent from child without rendering all children

Get parent from children in Django

Count and get children's in parent component

Cypress how to get all children element from the parent element but no grand children

Calling a children method of a functional component from parent

Call multiple children methods from parent component

Pass values from children to parent component