I am trying to grab all the input
elements that only exist after a boolean becomes true. So the div
is wrapped around an *ngIf
. I tried grabbing the elements using plain JavaScript, but it keeps returning empty. Here is my code:
test.component.html
<mat-checkbox (change)="toggleTest($event)">
Test check box
</mat-checkbox>
<div class="form-area" *ngIf="isTestChecked">
<input type="number">
<input type="text">
</div>
test.component.ts
isTestChecked = false;
toggleTest(event: any) {
this.isTestChecked = event.checked;
if (this.isTestChecked === true) {
const children = document.querySelectorAll('.form-area input');
console.log(children);
}
}
So the console.log
always prints an empty array. However, if I manually type the query selector in the browser console after setting the boolean to true, then it returns both of the input
elements.
What am I doing wrong? How come it won't get the input elements after they become added to the DOM? Any help would be appreciated!