Home > database >  Conditional reset method "if disable ignore not disable reset" angular
Conditional reset method "if disable ignore not disable reset" angular

Time:03-04

<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>

Note:- On add() both fields should be reset but on edit only address field should be reset by without disturbing previous value in it (ignore disabled field)

ts file

reset(){ this.name=""; this.address="";

}

by doing this all fields are getting reset

CodePudding user response:

In your .ts file, only reset the address field not the name.

reset(): void{ 
   this.address = "";
}

If you want to reset only the address field, when name field is disabled you can do something like this:

reset(): void{ 
   if(!this.editData) {
     this.name = "";
   }
   this.address = "";
}

CodePudding user response:

reset(): void{ 
   if(!this.editData) {
     this.name = "";
   }else{
   this.address = this.formvalue.address;
}
}
  • Related