-1
Good morning! I am doing a function for each click of the user on a given button appear the field for him to add another phone. Follows image below:
This is the html snippet:
<div *ngFor="let phone of s.controls; let i = index" class="list-group list-group-flush">
<h3>Complementar Telefone</h3>
<div [formGroup]="phon" class="row">
<div class="col-12 col-sm-6 form-group">
<label class="form-label" for="phone">Celular <span class="text-danger">*</span> </label>
<input mask="(00) 00000-0000" formControlName="phone" type="text" class="form-control" id="celular" placeholder="">
</div>
</div>
</div>
<div class="d-flex justify-content-middle">
<button (click)="onChangePhone()" class="ml-auto mr-auto btn btn-outline-info pt-2 pb-2 btn-sm mb-2" type="button"> + Adicionar Telefone </button>
</div>
</form>
This is the comm section:
public basicInfo: FormGroup;
}
this.basicInfo = this.formBuilder.group({
phone: [this.currentUser.user.phone, Validators.required],
});
get s() { return this.basicInfo.controls; }
get p() { return this.s.phones as FormArray; }
ngOnInit() {}
setBasicInfo(basicInfo) {
this.basicInfo.patchValue({
numberOfPhones: basicInfo.length
})
basicInfo.forEach(element => {
this.p.push(this.formBuilder.group({
phone: element.phone,
}));
});
}
onChangePhone(){
//return console.log('Testando')
const numberOfPhones = this.basicInfo.value.numberOfPhones;
this.basicInfo.patchValue({
numberOfPhones: numberOfPhones + 1
})
if (this.p.length < numberOfPhones) {
for (let i = this.p.length; i < numberOfPhones; i++) {
this.p.push(this.formBuilder.group({
phone: ['', Validators.required],
}));
}
} else {
for (let i = this.p.length; i >= numberOfPhones; i--) {
this.p.removeAt(i);
}
}
}
When I click the Add Phone button the following error appears:
Can someone please get me?
you have to use formArray inves of the group
– Eduardo Vargas
You refer to yourself in html?
– Sena Oliveira