【问题标题】:angular typeahead filter error "undefined is not a function"角度预输入过滤器错误“未定义不是函数”
【发布时间】:2014-11-03 19:11:01
【问题描述】:

我在这里有一个有效的 plunkr:plunkr,它定义并使用了一个过滤器,该过滤器将一个项目附加到预先输入的建议列表中。当我尝试将其合并到我的项目中时,它会在过滤器周围引发错误。这是我的代码:

html:

<div class="input-group" ng-controller="TypeaheadCtrl">
            <input type="text" ng-model="search" placeholder="Search for vendors or products" class="form-control"
               ng-bind-html="match.label | typeaheadHighlight:query"
              typeahead="product for product in getProductSuggestions($viewValue) | filter:$viewValue | finalAppend:$viewValue">

脚本:

var app = angular.module('clientApp')
app.controller('TypeaheadCtrl', function ($scope, $http, StateService) {
    $scope.getProductSuggestions = function(val) {
        return $http.get(StateService.getServerAddress() + "search/autocomplete?q=" + val
            ).then(function(response){
            return response.data.products;
        });
    };
  })

app.filter('finalAppend', function($sce){
      return function(array, value){
        array.push(  // error: undefined is not a function!
            $sce.trustAsHtml('Look for <b>' + value + '</b> in other shops')
        ); 
        return array;
      }
})

提前致谢。

编辑:最后我刚刚摆脱了过滤器,当我得到结果时,我将最后一件事附加到承诺本身。

【问题讨论】:

    标签: angularjs angular-ui-bootstrap angular-bootstrap angular-ui-typeahead


    【解决方案1】:

    问题是函数getProductSuggestions 没有检索数组。那是因为该函数正在发出一个 asinc 请求,并且您在 then 承诺的函数中设置了 return

    你需要修改你的代码。

    在你看来,这样做:

    <input type="text" ng-model="search" placeholder="Search for vendors or products" 
      class="form-control" 
      ng-change="updateProductSuggestions(search)" 
      ng-bind-html="match.label | typeaheadHighlight:query"
      typeahead="product for product in productSuggestions | filter:$viewValue | finalAppend:$viewValue">
    

    在你的控制器中这样做:

    app.controller('TypeaheadCtrl', function ($scope, $http, StateService, $timeout) {
        $scope.productSuggestions =[];
        $scope.updateProductSuggestions= function(val) {
            return $http.get(StateService.getServerAddress() + "search/autocomplete?q=" + val
                ).then(function(response){
                $timeout(function(){
                      $scope.productSuggestions = response.data.products;
                });
            });
        };
      })
    

    另外,请确保您在 response.data.products 中获得的是一个有效的数组。

    最后,您还可以像这样改进您的$filter

    app.filter('finalAppend', function($sce){
          return function(array, value){
            if(!(array && value))
                return array;
            array.push(
                $sce.trustAsHtml('Look for <b>' + value + '</b> in other shops')
            ); 
            return array;
          }
    })
    

    【讨论】:

    • 嗯我不明白如果不是数组和值将如何帮助使其正常工作?在我看来,如果无论如何都会引发错误,但你能否详细说明为什么它不应该?
    • @Josep, ng-change="updateProductSuggestions($viewValue)" 为 val 传递未定义的值。另外我不明白当我不使用过滤器时,promise 方法是如何工作的。
    • @Riz ups!给我一秒钟,让我解决这个问题
    • @Riz 应该是ng-change="updateProductSuggestions(search)"
    • @rahpuser if 声明不存在是为了解决问题,但要确保如果自定义 $filter 未与正确的参数一起使用,则不会引发异常。如果没有if 语句,如果过滤器与未定义的Array 一起使用,$filter 将抛出错误,同样如果value 的值为undefined,则附加最后一行没有意义,因此应该检索原始的Array
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多