angular - 如何清除选择值后?
问题描述
我有自动完成和表格。选择一个项目后,我需要将其添加到数组中并将其从自动完成中删除。
下面的代码不起作用
<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;
}
员工是对象
解决方案
您需要在更改后清空员工属性以使其为空。假设它是字符串,我已经为其分配了一个空值。
您可以根据其数据类型将其分配给空值。
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 = {};
}
推荐阅读
- postman - 邮递员的 OR 运算符
- apache-kafka-streams - 具有 KTable Binder 配置的 Spring Cloud Stream - 引发 java.lang.IllegalArgumentException:方法必须是声明性的
- node.js - Heroku:找不到模块'pg'?
- medical - CCD和CDA有什么区别
- python-3.x - RuntimeError:尝试反序列化 CUDA 设备上的对象,但 torch.cuda.is_available() 为 False。pytorch 未检测到 GPU
- java - 当我尝试使用光标加载器打开另一个活动时应用程序崩溃
- angular - 动态修改延迟加载的路由不起作用
- java - 如何在 Unity Android 插件库中访问 AlertDialog 的消息和标题?(我在 AlertDialog 中使用自定义字体)
- xcode - 使用当前时间提前 1.5 小时计算时间并将其显示在列表中
- node.js - Jenkins - /usr/bin/env: 节点:没有这样的文件或目录