angular - Select2 查询服务为空
问题描述
我会为我的 select2 下拉菜单自定义搜索。在搜索字段中写入 3 个字符后,我将使用这些搜索参数执行服务调用。
当我选择下拉菜单时,它显示错误 TypeError (Cannot read property 'machineService' of null) 在调用 getSelectOptions-Method 之前初始化服务。
HTML:
<select2 id="inputMachine"
[data]="machinesModel.data"
[options]="machinesModel.options"
[width]="'100%'"
[disabled]="machinesModel.isDisabled()"
(valueChanged)="machinesModel.onValueChanged($event); onSelectedMachinesChanged($event)">
</select2>
零件:
protected getSelectOptions(placeholder: string) {
return {
allowClear: false,
placeholder: this.translate.instant(placeholder),
multiple: true,
minimumInputLength: 3,
theme: 'bootstrap',
query: function (options) {
this.machineService.findProjections(options.term).subscribe(
machines => {
this.setMachines(machines);
options.callback(this.machinesModel.data);
},
error => {
console.log('Could not load machines: ', error);
}
);
}
};
}
有人知道吗?
解决方案
范围问题。
使用胖箭头摆脱它。
protected getSelectOptions(placeholder: string) {
return {
allowClear: false,
placeholder: this.translate.instant(placeholder),
multiple: true,
minimumInputLength: 3,
theme: 'bootstrap',
query: (options) => {
this.machineService.findProjections(options.term).subscribe(
machines => {
this.setMachines(machines);
options.callback(this.machinesModel.data);
},
error => {
console.log('Could not load machines: ', error);
}
);
}
};
}
推荐阅读
- php - bin/magento 设置:升级未运行 magento 2.3.X
- pivot - 默认情况下打开 ag-grid 透视总列
- postgresql - 普通PostgreSQL中选择语句中的光标
- node.js - 从外部浏览器将数据发送回电子桌面应用程序
- python - 从 Python 调用并执行 r 脚本
- python - 为 python 3.7 而不是 3.7.6 安装 matplotlib
- integration - Paytm 支付网关集成
- tarantool - 在 Tarantool 中迭代记录时删除记录会不会有任何问题?
- android - 在 Appium 运行时使用“UiAutomator dump”命令
- amazon-lex - 亚马逊 lex 插槽中断