I am new to angular, wanted to add address every time click the button "Add Address". I wanted it to be dynamic then I will submit.
HTML:
<div class="form-group">
<label for="name">Address: </label>
<div>
<button type="button" class="btn btn-info" (click)="addAddress()" >Add Address</button>
</div>
<div>
<ul>
<li *ngFor="let add of contact.address | async">
<label for="name">Type</label>
<input type="text" class="form-control" id="atype" required [(ngModel)]="add.type" name="atype">
....
</li>
</ul>
</div>
</div>
TS:
addAddress() {
let add = {
id: 0,
type: '',
number: 0,
street: '',
unit: '',
city: '',
state: '',
zipcode: ''
};
this.contact.Address.push(add);
}
when I click it does nothing but when I submit
I got empty arrays created:
Address: [{id: 0, type: "", number: 0, street: "", unit: "", city: "", state: "", zipcode: ""},…]
0: {id: 0, type: "", number: 0, street: "", unit: "", city: "", state: "", zipcode: ""}
1: {id: 0, type: "", number: 0, street: "", unit: "", city: "", state: "", zipcode: ""}
2: {id: 0, type: "", number: 0, street: "", unit: "", city: "", state: "", zipcode: ""}