Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angularjs bootstrap typeahead无效:错误:[filter:notarray]大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用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;
       }
       );
}

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">

为什么它不起作用:

异步提取项目数组时会发生此问题.在你的情况下,getItems函数被称为getListofNames,由于调用了$http,你的项目确实是异步获取的.因此,在发生错误时,getListofNames()仍然是未解析的promise对象,而不是@L_801_14@数组.

你怎么能做这个工作

从模板中删除过滤器.您应该在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
        }
    );
};

大佬总结

以上是大佬教程为你收集整理的Angularjs bootstrap typeahead无效:错误:[filter:notarray]全部内容,希望文章能够帮你解决Angularjs bootstrap typeahead无效:错误:[filter:notarray]所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。