简体   繁体   English

Angularjs错误:[filter:notarray]预期数组但收到:{}带有ng-repeat的过滤器

[英]Angularjs Error: [filter:notarray] Expected array but received: {} with a filter on an ng-repeat

I am attempting to populate an html table using an angular request to an API using the ng-repeat directive. 我正在尝试使用ng-repeat指令使用对API的角度请求来填充html表。 The html page loads first then the request is made to get the data which fills the table when the response is returned. 首先加载html页面,然后发出请求以获取返回响应时填充表的数据。 When I add a filter to the ng-repeat directive the table is populated and the filter functions, however in my chrome browser console I get the following error: 当我向ng-repeat指令添加过滤器时,表格被填充并且过滤器起作用,但是在我的chrome浏览器控制台中,我收到以下错误:

Error: [filter:notarray] Expected array but received: {} http://errors.angularjs.org/1.4.3/filter/notarray?p0=%7B%7D at REGEX_STRING_REGEXP (angular.js:68) at angular.js:18251 at Object.fn (app.js:185) at Scope.$get.Scope.$digest (angular.js:15683) at Scope.$get.Scope.$apply (angular.js:15951) at bootstrapApply (angular.js:1633) at Object.invoke (angular.js:4450) at doBootstrap (angular.js:1631) at bootstrap (angular.js:1651) at angularInit (angular.js:1545) 错误:[filter:notarray]预期数组但收到:{} http://errors.angularjs.org/1.4.3/filter/notarray?p0=%7B%7D在REGEX_STRING_REGEXP(angular.js:68)处于角度。 js:18251 at Scope.fn(app.js:185)at Scope。$ get.Scope。$ digest(angular.js:15683)at Scope。$ get.Scope。$ apply(angular.js:15951)at bootstrapApply (angular.js:1633)在Object.invoke(angular.js:4450)atBootstrap(angular.js:1631)at bootstrap(angular.js:1651)at angularInit(angular.js:1545)

I have set a sample up on plunker, the error is also displayed in the console here when the sample is run: 我在plunker上设置了一个样本,当运行样本时,错误也会显示在控制台中:

http://plnkr.co/edit/J83gVsk2qZ0nCgKIKynj ? http://plnkr.co/edit/J83gVsk2qZ0nCgKIKynj

The html: html:

<!DOCTYPE html>
<html>
<head>
  <script data-require="angular.js@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
  <script data-require="angular-route@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular-route.js"></script>
  <script data-require="angular-resource@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular-resource.js"></script>
  <script type="text/javascript" src="example.js"></script>
  <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet" />
</head>
<body ng-app="inventoryManagerApp">
  <h3>Sample - Expected array error</h3> Filter
  <input type="text" id="quoteListFilter" class="form-control" ng-  model="search" />
  <div ng-controller="QuoteController">
    <table class="table table-bordered">
      <tbody>
        <tr>
          <th>Specification</th>
          <th>Quantity</th>
        </tr>
        <tr ng-repeat="quote in quotes | filter:search">
          <td>{{quote.SpecificationDetails}}</td>
          <td>{{quote.Quantity}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>

The javascript: javascript:

var inventoryManagerApp = angular.module('inventoryManagerApp', [
  'ngResource',
  'quoteControllers'
]);

var quoteControllers = angular.module('quoteControllers', []);

quoteControllers.controller("QuoteController", ['$scope', 'filterFilter', 'quoteRepository',
  function($scope, filterFilter, quoteRepository) {

     $scope.quotes = quoteRepository.getQuoteList().$promise.then(
            function (result) {
                $scope.quotes = result;
            },
            function () {
            }
        );
  }
]);

inventoryManagerApp.factory('quoteRepository',
  function($resource) {
    return {
      getQuoteList: function() {
        return    $resource('http://drbsample.azurewebsites.net/api/Quotes').query();
      }
    };
  });

It seems to be something to do with the data to fill the ng-repeat directive not being immediately available on page load. 这似乎与填充ng-repeat指令的数据有关,不能立即在页面加载时使用。 When I replace the $scope.quotes with the json data on page load instead of requesting data from the API do not get the error. 当我在页面加载时使用json数据替换$ scope.quotes而不是从API请求数据时,不会收到错误。

The problem is with this assignment: 问题出在这个任务上:

$scope.quotes = quoteRepository.getQuoteList().$promise.then(
        function (result) {
            $scope.quotes = result;
        },
        function () {
        }
    );

Function .then() returns another promise object to allow chaining: .then().then() , and because it returns an object that's why your receive notarray error. 函数.then()返回另一个promise对象以允许链接: .then().then() ,因为它返回一个对象,这就是你接收notarray错误的原因。

To avoid reference error you can specify $scope.quotes as empty arrray earlier, then assign results to it. 为避免引用错误,您可以$scope.quotes指定为空arrray,然后将结果分配给它。

$scope.quotes = [];
quoteRepository.getQuoteList().$promise.then(
        function (result) {
            $scope.quotes = result;
        },
        function () {
        }
    );
$scope.quotes = quoteRepository.getQuoteList().$promise.then(

the assignment is useless. 任务没用。 just delete the $scope.quotes = from the line has to solve your problem. 只需从行中删除$scope.quotes =即可解决您的问题。

promise.then is returning an object which is useless for repeat statement. promise.then返回一个对重复语句无用的对象。

The $http legacy promise methods .success and .error have been deprecated and will be removed in Angular v1.6.0. $ http遗留承诺方法.success和.error已被弃用,将在Angular v1.6.0中删除。 Use the standard .then method instead. 请改用标准.then方法。

Now .then method returns object with several elements: data, status, etc. So you need to use response.data instead of just response: 现在.then方法返回带有几个元素的对象:数据,状态等。所以你需要使用response.data而不仅仅是响应:

$http.get('https://example.org/...')
  .then(function (response) {

    console.log(response);

    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;

    console.log(data);

});
quoteRepository.getQuoteList().then(
    function (result) {
        $scope.quotes = result;
    },
    function () {
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM