Error using ngTagInput : TypeError: a.filter is not a function

£可爱£侵袭症+ 提交于 2019-12-11 05:47:41

问题


I am trying to use ngTagInput with autocomplete but getting following Error:

angular.min.js:117 TypeError: a.filter is not a function
at d (ng-tags-input.min.js:1)
at ng-tags-input.min.js:1
at angular.min.js:130
at n.$eval (angular.min.js:144)
at n.$digest (angular.min.js:142)
at n.$apply (angular.min.js:145)
at l (angular.min.js:97)
at H (angular.min.js:101)
at XMLHttpRequest.u.onload (angular.min.js:102)

HTML

<tags-input ng-model="selectedList">
  <auto-complete source="getData($query)"></auto-complete>
</tags-input>

Javascript

$scope.getData = function(query) { 
  var request = {
    // GET request is defined here
  };

  return $http(request).success(function(response, status) {
    var defer = $q.defer();
    defer.resolve([
      { 'text': 'just' },
      { 'text': 'some' },
      { 'text': 'cool' },
      { 'text': 'tags' }
    ]);
    return defer.promise;
  });
};

Plunker


回答1:


You're using the success method, that isn't chainable, i.e. it ignores return values, so the getData function is, in fact, returning the $http promise directly, which according to your Plunker, happens to resolve into an undetermined value, hence the filter is not a function error, since the autoComplete directive expects an array.

You should use then instead of success (you also don't need to create a new promise in order to return a value from inside another promise):

$scope.getData = function(query) { 
  ...
  return $http(request).then(function(response, status) {
    return [
      { 'text': 'just' },
      { 'text': 'some' },
      { 'text': 'cool' },
      { 'text': 'tags' }
   ];
  });
}

Updated Plunker

That'll work, but it doesn't make much sense because it issues a request and then discards its response. If you don't need to handle the response, you can simply return the http promise directly (assuming the response structure is valid for the directive):

$scope.getData = function(query) { 
  ...
  return $http(request);
};

Finally, both success and error methods have been deprecated. You should always use then instead.



来源:https://stackoverflow.com/questions/38601519/error-using-ngtaginput-typeerror-a-filter-is-not-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!