gpt4 book ai didi

javascript - Angular 错误 : [filter:notarray] Expected array but received: {} with a filter on an ng-repeat

转载 作者:可可西里 更新时间:2023-11-01 01:56:41 31 4
gpt4 key购买 nike

我正在尝试使用 ng-repeat 指令使用对 API 的 Angular 请求来填充 html 表。首先加载 html 页面,然后请求获取返回响应时填充表格的数据。当我向 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)

我已经在 plunker 上设置了一个示例,当示例运行时,错误也会显示在此处的控制台中:

http://plnkr.co/edit/J83gVsk2qZ0nCgKIKynj

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>

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();
}
};
});

这似乎与填充 ng-repeat 指令的数据在页面加载时不能立即可用有关。当我在页面加载时将 $scope.quotes 替换为 json 数据而不是从 API 请求数据时,不会收到错误。

最佳答案

问题出在这个作业上:

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

函数 .then() 返回另一个允许链接的 promise 对象:.then().then(),并且因为它返回一个对象,这就是为什么您收到 notarray 错误。

为避免引用错误,您可以先将 $scope.quotes 指定为空数组,然后将结果分配给它。

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

关于javascript - Angular 错误 : [filter:notarray] Expected array but received: {} with a filter on an ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32214990/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com