here is my problem, I used ngModelChange function to trigger and event. when i give a number of groups, it gives a number of input fields below that. but that new input fields only allow one integer. if I want to enter another number, I have to click the input field again and again. as an example if I want to input 100. I have to click the same input field 3 times.
here is the html part
<div class="row">
<div class="col-md-12">
<label>Number of Groups </label>
<input type="number" name="numberofGroups" #numberofGroups="ngModel" [(ngModel)]="StudentCountservice.formData.numberofGroups" (ngModelChange)="onValueChange($event)"class="form-control" required>
</div>
</div>
<div class="row" *ngFor="let grp of groups;let i=index " [attr.data-index]="i" >
<div class="col-md-12">
<label>Sizes of Group {{i+1}} </label>
<input type="number" name="sizeofGroups" #sizeofGroups="ngModel" [(ngModel)]="groups[i]" class="form-control"ng-model-option="{debounce:750}" required >
</div>
</div>
here is the ts file code part
onValueChange(newvalue){
this.groups=[];
for(var i=0;i<newvalue;i++){
this.groups[i]='';
}
}
here is the stackblitz example
I found this answer (https://github.com/angular/angular/issues/20589). I dropped the following code into your stackblitz and it worked. Give it a try and let me know if you can't get it to work.
Here is my stackblitz fork: https://stackblitz.com/edit/angular-sa8ww6
app.component.ts
// imports and metadata
export class AppComponent {
name = 'Angular';
groups:any =[];
onValueChange(newvalue){
this.groups=[];
for(var i=0;i<newvalue;i++){
this.groups[i]='';
}
}
// Add this function
trackArray(index, item) {
return index;
}
}
app.component.html
Added trackBy: trackArray
to your ngFor
loop
<div class="row">
<div class="col-md-12">
<label>Number of Groups </label>
<input type="number" name="numberofGroups" #numberofGroups="ngModel" [(ngModel)]="test" (ngModelChange)="onValueChange($event)"class="form-control" required>
</div>
</div>
<!-- add the trackBy: trackArray to this ngFor -->
<div class="row" *ngFor="let grp of groups;let i=index; trackBy: trackArray " [attr.data-index]="i" >
<div class="col-md-12">
<label>Sizes of Group {{i+1}} </label>
<input type="number" name="sizeofGroups" #sizeofGroups="ngModel" [(ngModel)]="groups[i]" class="form-control"ngModelOptions="{updateOn: 'blur'}" required >
</div>
</div>