Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through @ViewChildren native elements

Tags:

angular

Given the following markup

<div *ngFor="let item of items">
    <input type="text" #val />
</div>

I defined in a component

@ViewChildren('val') rows;

And I disable the first input element with

this.rows.first.nativeElement.disabled = true;

How can I loop to disable all the inputs?

This doesn't work

this.rows.forEach(val => val.disabled = true);

and this doesn't work either

this.rows.forEach(val => val.nativeElement.disabled = true);
like image 738
ps0604 Avatar asked Sep 06 '26 01:09

ps0604


1 Answers

To loop through the children, use toArray() like so:

this.rows.toArray().forEach(val => val.nativeElement.disabled = true);
like image 137
ps0604 Avatar answered Sep 07 '26 18:09

ps0604



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!