gpt4 book ai didi

javascript - Angularjs 处理没有 $scope 的 promise

转载 作者:行者123 更新时间:2023-11-27 22:31:01 24 4
gpt4 key购买 nike

我有一个工厂,可以为 Restful api 创建 SubCon $resource。

app.factory("SubCon", function($resource) {
return $resource("http://localhost:8010/api/subcontractors/:id");
})

我有一个使用该服务的 Controller ,并直接将通过查询服务返回的 promise 传递给 ng-repeat。

//in the controller
$scope.partners = this.SubCon.query();

这是模板中的重复内容:

<tr ng-repeat="partner in partners">
<td>{{partner.id}}</td>
<td>{{partner.name}}</td>
<td>?</td>
<td>?</td>
<td>---</td>
<td>?</td>
</tr>

这效果很好并且输出了我所期望的结果。因此,我们制定了禁止使用 $scope 的政策,因此我重构了 Controller 和模板,如下所示:

//in the controller
this.getPartners = function() {
return this.SubCon.query();
}

模板:

<tr ng-repeat="partner in ctrl.getPartners()">
<td>{{partner.id}}</td>
<td>{{partner.name}}</td>
<td>?</td>
<td>?</td>
<td>---</td>
<td>?</td>
</tr>

现在,当我使用 Angular 1.5.8 运行此代码时,我收到一个摘要错误,如下所示。

那么我错过了什么。为什么这适用于 $scope 而不是使用 Controller 上的方法?

angular.js:13920 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.5.8/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.js:68
at Scope.$digest (angular.js:17562)
at Scope.$apply (angular.js:17790)
at done (angular.js:11831)
at completeRequest (angular.js:12033)
at XMLHttpRequest.requestLoaded (angular.js:11966)(anonymous function) @ angular.js:13920
angular.js:17793 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.5.8/$rootScope/infdig?p0=10&p1=%5B%5D

最佳答案

$scope.partners = this.SubCon.query(); 替换为 this.partners = this.SubCon.query();

然后就可以正常传递一个集合给ng-repeat了:

<tr ng-repeat="partner in ctrl.partners">

关于javascript - Angularjs 处理没有 $scope 的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39580144/

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