我对自己的代码有疑问。
我想在我的国家/地区plugin中使用此自动完成功能
我得到JSON中的所有国家/地区,如下所示:
JS: country [{
JS: "name": "Afghanistan",
JS: "short_name": "AF",
JS: "country_id": 1
JS: }, {
JS: "name": "Albania",
JS: "short_name": "AL",
JS: "country_id": 2
JS: }, {
JS: "name": "Algeria",
JS: "short_name": "DZ",
JS: "country_id": 3
JS: }, {
JS: "name": "American Samoa",
JS: "short_name": "AS",
JS: "country_id": 4
JS: }, {
JS: "name": "Andorra",
JS: "short_name": "AD",
JS: "country_id": 5
JS: },{
JS: "...}]
在使用中,我像下面的代码中那样调用函数:
public getAllCountryws(): Observable<Country[]> {
return this.http.get(Api.getUrl(Api.URLS.countryGetAll), {
})
.pipe(map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else if (res.StatusCode === 4) {
return false;
} else if (res.StatusDescription === 'No result') {
return false;
} else if (res.StatusDescription === '[]') {
return false;
} else if (res.StatusDescription === []) {
return false;
} else {
return res.StatusDescription.map(country => {
return new Country(country);
});
}
},
(error) => {
console.log(error);
}))
}
在component.ts中,我像下面的代码一样调用服务函数:
public country: Country[] = [];
private _items: ObservableArray<TokenModel>;
@ViewChild("autocomplete") autocomplete: RadAutoCompleteTextViewComponent;
ngOnInit(): void {
this.getallcountry();
}
getallcountry() {
this.ws.getAllCountryws().subscribe(
country => {
this.country = country;
const mycountry = country;
console.log('mycountry', mycountry) // show correct JSON
for (let i = 0; i < mycountry.length; i++) {
console.log(mycountry.length) // show correct
this._items.push(new TokenModel(mycountry[i].company_id, null));
}
},
err => console.error('err', err),
() => console.log('error')
);
}
错误:
此外,在html中,我有以下代码:
<Label text="Select Country*" row="0" col='0'></Label>
<RadAutoCompleteTextView #autocomplete [items]="_items" suggestMode="Suggest" displayMode="Tokens"
row="1" col='0' hint="Country">
<SuggestionView tkAutoCompleteSuggestionView>
<ng-template tkSuggestionItemTemplate let-item="item">
<StackLayout orientation="vertical" padding="10">
<Label [text]="item.name"></Label>
</StackLayout>
</ng-template>
</SuggestionView>
</RadAutoCompleteTextView>
在视图中,什么也不显示。找不到结果。
更新:
发件人:private _items: ObservableArray<TokenModel>;
收件人:public _items: TokenModel[] = [];
添加:
get dataItems(): TokenModel[] {
console.log('this._items', this._items)
return this._items;
}
在html中进行更改:
来自:<RadAutoCompleteTextView #autocomplete [items]="_items" suggestMode="Suggest" displayMode="Tokens"
至:
<RadAutoCompleteTextView #autocomplete [items]="dataItems" suggestMode="Suggest" displayMode="Tokens"
视图中不显示任何结果。
答案 0 :(得分:1)
在component.ts文件中更改_items
声明
发件人:
private _items: ObservableArray<TokenModel>;
收件人:
private _items: TokenModel[] = [];