已达到AngularJS 10$digest()迭代次数。尝试使用$watch使服务、控制器和视图保持同步是可行的,但会导致错误

已达到AngularJS 10$digest()迭代次数。尝试使用$watch使服务、控制器和视图保持同步是可行的,但会导致错误,angularjs,service,controller,watch,digest,Angularjs,Service,Controller,Watch,Digest,好的,我知道这里的问题是什么。我使用的是$watch,由于我设置的值(通过调用我的服务中的函数),每次都会创建一个新数组,手表会不断地找到一个新值。但我的问题是,如何解决这个问题。我当前的设置如下(仅包括代码的相关部分): clientCtrl.js opportunityService.js .service('opportunityService',函数($http){ this.getOpportunities=函数(id){ 这个结果=[]; 如果(id的类型==“未定义”){ this

好的,我知道这里的问题是什么。我使用的是$watch,由于我设置的值(通过调用我的服务中的函数),每次都会创建一个新数组,手表会不断地找到一个新值。但我的问题是,如何解决这个问题。我当前的设置如下(仅包括代码的相关部分):

clientCtrl.js opportunityService.js
.service('opportunityService',函数($http){
this.getOpportunities=函数(id){
这个结果=[];
如果(id的类型==“未定义”){
this.results=this.opportunities;
}
否则{

对于(var a=0;a如果我正确理解您的问题,您应该使用$watch而不是$watch

服务

getOpportunities: function(id) {

    // $http.get returns a promise itself.
    // With the $q object you can write more complex code if needed
    var deferred = $q.defer();
        $http.get("/what/ever")
        .success(function(data) { 
            deferred.resolve(data);
        })
        .error(function() {
            deferred.reject("Oops");
        });
    return deferred.promise;
}
控制器

var promise = opportunityService.getOpportunities($scope.id);
promise.then(
    function(p) { 
        $scope.opportunities = p.data;
    },
    function(data) {
        alert("error");
    });
};
模式如何修改(添加或删除)Opportunity列表?如果
ClientCtrl
启动模式,它应该获得模式结果的承诺。您能否使用该结果更新
ClientCtrl
中的
$scope.opportunities
getOpportunities: function(id) {

    // $http.get returns a promise itself.
    // With the $q object you can write more complex code if needed
    var deferred = $q.defer();
        $http.get("/what/ever")
        .success(function(data) { 
            deferred.resolve(data);
        })
        .error(function() {
            deferred.reject("Oops");
        });
    return deferred.promise;
}
var promise = opportunityService.getOpportunities($scope.id);
promise.then(
    function(p) { 
        $scope.opportunities = p.data;
    },
    function(data) {
        alert("error");
    });
};