Angular 6重置选择选项onclick

问题描述 投票:1回答:1

我在点击按钮时尝试重置选项值。我无法找到任何解决方案。我有一个组件,我正在使用此代码:

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

我的课程内部使用此代码:

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

在循环内调用选项。我在按钮事件上使用此代码:

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

我应该怎么做才能使其默认或为null

select angular6 reset angular-forms
1个回答
1
投票

你的逻辑是正确的,但实现是错误的:

appendToContainer(){
    // logic 
    this.options == null; // ==> WRONG expression
    this.options = []; // ==> CORRECT
}
© www.soinside.com 2019 - 2024. All rights reserved.