1 回答
TA贡献1795条经验 获得超7个赞
实际上有两种方式来做
使用[(ngMode)]l来做双向绑定
[value]只是单向绑定
<div class="form-group row">
<div class="col-md-8">
<input type="text" id="title" placeholder="Please input article title" [(ngModel)]="article.title"">
</div>
<button class="btn btn-primary" (click)="onCancel()">Cancel</button>
</div>
article: any = {
title: ""
};
onCancel(): void {
this.article.title = "";
//或者
this.article.title = "";
this.article = Object.assign({}, this.article);
}
在做这个Object.assign(targetObject, sourceObject)之前,一定要先把title置空啊,要不然你只是copy了一下object, input当然没有被清空了。
使用angular Form来做
<div class="form-group row" [formGroup]="formGroup">
<div class="col-md-8">
<input type="text" id="title" placeholder="Please input article title" formControlName="title">
</div>
<button class="btn btn-primary" (click)="onCancel()">Cancel</button>
</div>
formGroup: FormGroup;
ngOnInit() {
this.formGroup = new FormGroup({});
this.formGroup.addControl("title", new FormControl());
}
onCancel(): void {
this.formGroup.get("title").setValue("", true);
//或者
this.formGroup.get("title").reset("");
}
添加回答
举报
