首页 > 解决方案 > 如何清除选择值后?

问题描述

我有自动完成和表格。选择一个项目后,我需要将其添加到数组中并将其从自动完成中删除。

下面的代码不起作用

<p-autoComplete
  (completeMethod)="getEmployees($event.query)"
  (onSelect)="change($event)"
  [(ngModel)]="employee"
  [suggestions]="employees"
  field="title"
  minLength="0"
></p-autoComplete>


change(newVal): void {
  if (!newVal) {
    return;
  }
  if (this.memberExists(newVal)) {
    this.employee = null;
    return;
  }
  this.document.employees.push(this.employee);
  this.employee = null;
}

员工是对象

标签: angularprimeng

解决方案


您需要在更改后清空员工属性以使其为空。假设它是字符串,我已经为其分配了一个空值。

您可以根据其数据类型将其分配给空值。

change(newVal): void {
  if (!newVal) {
    return;
  }
  if (this.memberExists(newVal)) {
    this.commission = null;
    return;
  }
  this.document.commission.push(this.commission);
  this.commission = null;
  // reset employee value to make it empty.
  this.employee = {};
}


推荐阅读