【问题标题】:I'm looking for a way to remove select option after its pushed to array in angular 2+我正在寻找一种在将选择选项推到角度 2+ 后删除选择选项的方法
【发布时间】:2019-12-20 06:29:28
【问题描述】:

我需要一种方法在元素添加到数组后从选择列表中删除,不允许再次插入,这是下拉列表:

<div class="modal-body">
      <select class="form-control select2-hidden-accessible" id="ddlGrupos" name="user Selecionado" [(ngModel)]="userSelected" (change)="selectValorUsuario(userSelected)">
        <option *ngFor="let usuario of Usuario" [ngValue]="usuario">{{usuario.nome}}</option>
      </select>
</div>

这是推送后必须从列表中删除的推送方法:

adicionarArrUsuario() {
   const result = this.unidadeUsuarioArr.some(t => t.id === this.usuarioObjectSelect.id);
   if (!result) {
      this.unidadeUsuarioArr.push({
      id: this.usuarioObjectSelect.id,
      nome: this.usuarioObjectSelect.nome
     });
   }
}

【问题讨论】:

  • 也许你可以disable option 如果它被添加到数组中?

标签: arrays angular loops ngfor


【解决方案1】:

您可以使用Array.filter 删除选择后的元素:

selectValorUsuario(userSelected) {
   this.Usuario = this.Usuario.filter(usuario => usuario.id !== userSelected.id);        
}

将此方法应用于您的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多