我正在使用ng2-select,似乎无法找到它。
GetCountriesWithNameAndID(){
this.countryService.GetCountriesWithNameAndID()
.subscribe((res: ICountry[]) => {
this._countries = res;
this._countries.forEach((country: ICountry) => {
//console.log(country.CountryID + "===========================" + country.CountryName);
this.items.push(country.CountryName);
//this.items.push({
// id: country.CountryID,
// text:country.CountryName
//})
this.select.itemObjects.push(new SelectItem({ id: country.CountryID, text: country.CountryName }));
})
})
}
我在这里找到了一个类似的问题,试图遵循回答,但被卡住
import { SelectItem } from 'ng2-select/components/select/select-item';
i出于某种原因使用" selectItem"对象绕过。不确定在NG2-Bootstrap实现中是否存在错误,对象未导出或任何其他冲突
因此,我围绕此操作,如以下片段中所示,从封装ng2-select功能中,该功能显示了获取远程数据的相关方法,并准备了它们以在Select中显示的。
import { Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
import { SelectComponent } from 'ng2-select';
import { Observable } from 'rxjs/Rx';
@Component({
selector: 'my-select',
template: `<ng-select
(active)="selected_item"
[allowClear] = "allowClear"
[items] = "myRecords"
[disabled] = "disabled"
(data) = "refreshValue($event)"
(selected) = "selected($event)"
(removed) = "removed($event)"
(typed) = "typed($event)"
placeholder = "{{placeholder}}">
</ng-select>`
})
export class mySelect {
constructor() {}
myRecords: any[];
mySelectItems: any[];
....
// get Items for select
getItems(searchstring: string) {
var self = this;
// fetch items from remote datasource
this.dataService.getRecords(this.webapi_data, this.pageno, this.pagesize, this.sortcolumn, this.data_filter, null, null)
.subscribe((data: any[]) => {
var response: any = data;
var myrecs = response.items;
// prepare fetched data for ng2-select datastructure and assign to bound array
this.myRecords = this.getData(myrecs);
},
error => {
console.log("MySelect: " + error);
});
}
// convert fetched to ng2-select datastructure
getData(data: any): any {
this.mySelectItems = [];
try {
for (let entry of data) {
var t: Object = {};
t['id'] = entry[this.valuefield];
t['text'] = entry[this.textfield];
this.mySelectItems.push(t);
}
} catch (e) {
console.log('my Select fill box:' + e);
}
return this.mySelectItems;
}
....
}
i用" selectitem"导入语句重新启动了视觉工作室,而所有问题都消失了。