条件重置方法 "if disable ignore not disable reset" angular
Conditional reset method "if disable ignore not disable reset" angular
<div>
<div>
<label>Name</label>
<input type="text" [(ngModel)]="name" [disabled]="editData"
</div>
<div>
<label>address</label>
<input type="text" [(ngModel)]="address"
</div>
</div>
<button (click)=add() >add</button>
<button (click)=edit()>edit</button>
<button (click)=reset()>reset</button>
注意:- 在 add() 时,两个字段都应重置,但在编辑时,仅地址字段应重置,而不干扰其中的先前值(忽略禁用字段)
ts 文件
reset(){ this.name=""; this.address="";
}
通过这样做,所有字段都将被重置
在您的 .ts
文件中,只重置地址字段而不是名称。
reset(): void{
this.address = "";
}
如果您只想重置地址字段,当名称字段被禁用时,您可以这样做:
reset(): void{
if(!this.editData) {
this.name = "";
}
this.address = "";
}
reset(): void{
if(!this.editData) {
this.name = "";
}else{
this.address = this.formvalue.address;
}
}
.ts 文件
add() {
this.name = '';
this.address = '';
}
edit() {
this.name = this.name;
this.address = '';
}
reset() {
if (!this.editData) {
this.name = '';
}
this.address = '';
}
<div>
<div>
<label>Name</label>
<input type="text" [(ngModel)]="name" [disabled]="editData"
</div>
<div>
<label>address</label>
<input type="text" [(ngModel)]="address"
</div>
</div>
<button (click)=add() >add</button>
<button (click)=edit()>edit</button>
<button (click)=reset()>reset</button>
注意:- 在 add() 时,两个字段都应重置,但在编辑时,仅地址字段应重置,而不干扰其中的先前值(忽略禁用字段)
ts 文件
reset(){ this.name=""; this.address="";
}
通过这样做,所有字段都将被重置
在您的 .ts
文件中,只重置地址字段而不是名称。
reset(): void{
this.address = "";
}
如果您只想重置地址字段,当名称字段被禁用时,您可以这样做:
reset(): void{
if(!this.editData) {
this.name = "";
}
this.address = "";
}
reset(): void{
if(!this.editData) {
this.name = "";
}else{
this.address = this.formvalue.address;
}
}
.ts 文件
add() {
this.name = '';
this.address = '';
}
edit() {
this.name = this.name;
this.address = '';
}
reset() {
if (!this.editData) {
this.name = '';
}
this.address = '';
}