- 角 1.5.8
- 引导程序 3.3.7 (CSS)
- Angular-ui 2.0.2
使用 angular Typeahead ( ui.bootstrap.typeahead ) 需要一个对象列表,它将在带有 HTML 的 ui 组件中显示
问题
从服务返回
promise
到组件(1.5 样式控制器、视图、绑定)控制器函数使用
promise
从服务返回并执行then
逻辑并返回array
对象Typeahead 不处理数组...执行控制台日志,您可以看到数组。
如果我在不使用服务方法的情况下静态传递相同的对象数组,那么该功能将起作用
HTML
<input type="text" ng-model="$ctrl.search.term" ng-disabled="!$ctrl.search.type"
typeahead-wait-ms="600"
placeholder="Search..."
uib-typeahead="res as res.name for res in $ctrl.submit($viewValue)"
typeahead-no-results="noResults" class="form-control" required>
<i ng-show="loadingLocations" class="icon ion-refresh"></i>
<div ng-show="noResults">
<i class="icon ion-close"></i> No Results Found
</div>
<select class="form-control custom-select-md"
ng-model="$ctrl.search.type"
placeholder="Type"
required>
<option value="" disabled selected>Select Type?</option>
<option value="car">car</option>
<option value="van">van</option>
</select>
组件(控制器、视图)
//submit search for issuers or issuedCard
submit() {
this.isSubmitting = true;
this._SearchService.performSearch(this.search)
.then(
(resp) => {
//e.g. [{id:1, name:'test'}]
console.log('Search Result', resp);
return resp;
},
(err) => {
console.log('Error Search', err);
this.reset(false);
this.errors = err;
return [];
}
);
//Comment out method above to see this static data returning and working as should be :'(
//return [{id:865,issuer: {},name:"British Testing"},
// {id:866,issuer: {},name:"American Testing"}];
}
服务
performSearch(searchData) {
console.log('Search Qry', searchData);
let deferred = this._$q.defer();
if(!this.isValidSearch(searchData)) {
deferred.reject({status:400, error: 'Bad Request', message:'invalid data'});
return deferred.promise;
}
let searchURI = (searchData.type === 'car' ? 'van' : 'issuer');
this._$http({
url: `${this._AppConstants.api}/${this._AppConstants[searchURI]['search']}`,
method: 'GET',
params: {
name: searchData.term
}
}).then(
(resp) => {
console.log('Search Data', resp);
this.result.term = searchData.term;
this.result.type = searchURI;
deferred.resolve(resp.data[this._AppConstants[searchURI]['searchResp']]);
},
(err) => {
console.log('Error performing search', err);
deferred.reject(err.data);
}
);
return deferred.promise;
}