2016-09-19 88 views
1

我有一個爲restful api創建SubCon $資源的工廠。

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

我有一個使用該服務的控制器,並直接將通過查詢服務返回的承諾傳遞給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> 

這個偉大的工程和輸出我的期望。 因此,我們必須對使用$範圍,所以我重構控制器和模板,這樣的政策:

//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> 

現在,當我採用了棱角分明1.5.8運行此代碼得到一個摘要錯誤,如下所示。

那麼我錯過了什麼。爲什麼這與$ scope一起使用,而不是在控制器上使用方法?

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 

回答

1

通過this.partners = this.SubCon.query();

更換$scope.partners = this.SubCon.query();然後,你可以通過一個集合通常爲NG而重複:

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