Search code examples
selectangular6resetangular-forms

Angular 6 reset select options onclick


I am trying to reset options value when clicked on button. I was unable to find any solution. I have a component where I am using this code:

 <select class='select-option' required [(ngModel)]='optionSelected' (ngModelChange)="toNumber()">
      <option [ngValue]="undefined" disabled  selected> Please select one option </option>
      <option *ngFor='let option of options' [ngValue]="option">{{option}}</option>
  </select>

Inside my class I am using this code:

options = ["Bar", "Pie", "Area"];

To call options inside a loop. I am using this code on button event:

appendToContainer(){
  // logic 
  this.options  == null
 }

What should I do to make it default or null


Solution

  • Your logic is correct, but implementation is wrong:

    appendToContainer(){
        // logic 
        this.options == null; // ==> WRONG expression
        this.options = []; // ==> CORRECT
    }