【发布时间】:2019-05-30 08:59:45
【问题描述】:
用户可以单击一个按钮,该按钮将添加更多地址表单字段。如果用户在字段中填写他们的地址信息,然后单击按钮添加更多地址,则之前的输入将被清除。如何防止按钮清除以前的条目?
来自我的 component.ts 文件的片段:
ngOnInit() {
this.addresses = [];
this.addresses.push(new Address('', undefined ,'','Canada',''));
}
addAddress(){
this.addresses.push(new Address('', undefined ,'','Canada',''));
}
我的地址.ts 文件:
export class Address {
constructor(
public streetAddress: string,
public province: string,
public city: string,
public country: string,
public postalCode: string
) { }
}
来自我的 component.html 文件的片段:
<div class="card-body" *ngFor="let address of addresses">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control"
[(ngModel)]="address.streetAddress" #streetAddress="ngModel"
name="streetAddress" placeholder="Apartment/House Number and Street
Address">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" [(ngModel)]="address.city"
#city="ngModel" name="city" placeholder="City/Town">
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-warning btn-block" id="addAddress"
(click)="addAddress()">More Addresses</button>
【问题讨论】:
标签: angular typescript