我正在使用ng2-select2 ajax的级联下拉列表,当我更改第一个下拉列表时,它应该重新呈现第二个下拉选项,但该选项不会重新呈现。第二个下拉列表将从第一个下拉值中获取@Input()的值并运行ajax。如果我使用setTimeout,它将重新渲染。反正有没有setTimeout吗?
第二次下拉
@Input() value: string;
public ngOnChanges(): void {
this.getConditionType(this.value);
}
public prepareUrl(value: string): void {
this.url = myUrl + '/' + pathVariable;
// setTimeout(() => { if I use timeout it will re render
this.setSelectOption(this.url);
// }, 1);
}
public setSelectOption(url: string): void {
let ajaxOptions = {
url: url,
dataType: 'json',
delay: 500,
cache: false,
data: (params: any) => {
return {
search: params.term,
gotoPage: params.page
};
},
processResults: (data: any, params: any) => {
params.page = params.page || 1;
return {
results: $.map(data.objectValue.items, function (obj) {
return { id: obj.code, text: obj.desc };
}),
pagination: {
more: (params.page * 10) < data.objectValue.total_count
}
};
},
};
this.options = {
ajax: ajaxOptions,
};
}
HTML
<select2 [options]="options"></select2>