dee*_*ght 4 twitter-bootstrap angularjs bootstrap-typeahead angular-ui-bootstrap angular-ui-typeahead
我需要使用typeahead进行http调用来获取建议选项
$scope.getlist = function getListofNames(name) {
return $http({
method: 'GET',
url: '/v1/'+name,
data: ""
}).then(function(response){
if(response.data.statusMessage !== 'SUCCESS'){
response.errorMessage = "Error while processing request. Please retry."
return response;
}
else {
return response.data.payload;
}
}, function(response){
response.errorMessage = "Error while processing request"
return response;
}
);
}
Run Code Online (Sandbox Code Playgroud)
response.data.payload是一个对象数组,它已成功获取但我收到此错误错误:[filter:notarray] http://errors.angularjs.org/1.4.5/filter/notarray?
注意:我使用的是角度1.4.5和Bootstrap v3.1.1
我猜你的typeahead标记看起来像这样:
<input [...] typeahead="item in getItems($viewValue) | filter: $viewValue">
Run Code Online (Sandbox Code Playgroud)
异步提取项目数组时会发生此问题.在你的情况下,getItems函数被调用getListofNames,并且您的项目确实是异步获取,由于呼叫$http.因此,在发生错误的时候,getListofNames()仍然是一个未解决的承诺对象,尚未名称的数组.
从模板中删除过滤器.您应该在返回数组之前过滤该数组getItems.理想情况下,您希望在服务器端进行过滤.实际上,服务器接收由用户键入的子字符串(这是$viewValue一个参数),因此,它拥有所有数据到阵列筛选.这样可以防止返回所有元素并缩短响应时间.或者,您可以在承诺的回调中过滤客户端:
$scope.getList = function getListofNames(name) {
return $http(...}).then(
function(response){
// filter response.data.payload according to
// the 'name' ($viewValue) substring entered by the user
return filteredArray; // <- no need to pipe a filter in the template anymore
}
);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1249 次 |
| 最近记录: |