Javascript AngularJS http承诺发射4次

Javascript AngularJS http承诺发射4次,javascript,angularjs,Javascript,Angularjs,我使用AngularJS调用一个http服务,该服务在对象中返回一些打开时间。我不明白为什么在我的控制器中,console.log打印了4次,而不是一次。谁能给我解释一下吗 这是我的服务/工厂代码: myApp.factory('BookingFactory', ['$http', '$q', function($http, $q) { var deferredTime = $q.defer(); return { GetDealerLocationTimeLi

我使用AngularJS调用一个http服务,该服务在对象中返回一些打开时间。我不明白为什么在我的控制器中,console.log打印了4次,而不是一次。谁能给我解释一下吗

这是我的服务/工厂代码:

myApp.factory('BookingFactory', ['$http', '$q', function($http, $q) {
    var deferredTime = $q.defer();

    return {
        GetDealerLocationTimeList: function(websiteId) {
            return $http.get('/d/GetDealerLocationTimes?website_id=' + websiteId)
                .then(function(response) {
                    deferredTime.resolve(response.data);
                    dealerLocationTimeList.push(response.data);
                    return deferredTime.promise;
                }, function(error) {
                    deferredTime.reject(response);
                    return deferredTime.promise;
                });
        }
    }
}]);
以下是调用服务的控制器代码:

var promise = BookingFactory.GetDealerLocationTimeList(website_id);

promise.then(
    function(da) {
        $scope.dealerLocationTimeList = da;
        console.log($scope.dealerLocationTimeList);
    },
    function(error) {
        $log.error('failure loading dealer associates', error);
    }
);

这段代码中有很多错误>除了一个承诺中的4个日志几乎不可信之外,这
延迟时间
应该做什么?在我看来,这是非常接近的,在错误的情况下,范围中甚至没有响应。你确定这是你代码中唯一的
控制台.log
语句吗?Bergi,我从某个地方的一个示例中得到了这种做承诺的方法。如果我没有正确地进行异步调用,您能告诉我正确的方法吗?我是个新手,谢谢你的建议。我意识到我的控制器被多次调用,这就是为什么我从console.log获得多个输出。我已删除对BookingController的所有引用,除了一个。但它仍然被调用了两次。不确定这是否是angular-ui-router引起的路由问题。您可以使用开发人员工具中的断点来检查代码的特定部分是否被多次调用。请注意,您可以(也应该)简单地忽略
函数(error){$q.reject(error);}
回调到
然后
,这样错误就会自动传播。或者如果没有,则将缺少的
返回值添加到其中。@Bergi yes在这种情况下,应忽略拒绝部分。更新答案以反映它。
myApp.factory('BookingFactory', ['$http', '$q', function($http, $q) {
    return {
        GetDealerLocationTimeList: function(websiteId) {
            var deferredTime = $q.defer(); // deferred should be created each time when a function is called. It can only be consumed (resolved/rejected) once.

        /* return - don't need to return when you already creating a new deferred*/ 
            $http.get('/d/GetDealerLocationTimes?website_id=' + websiteId)
                .then(function(response) {
                    deferredTime.resolve(response.data);
                    // dealerLocationTimeList.push(response.data);
                }, function(error) {
                    deferredTime.reject(error); // it should be 'error' here because your function argument name says so...
                });

           return deferredTime.promise; // promise is returned as soon as after you call the function, not when the function returns
        }
    }
}]);
myApp.factory('BookingFactory', ['$http', '$q', function($http, $q) {
    return {
        GetDealerLocationTimeList: function(websiteId) {
            // no need to create new deferred anymore because we are returning the promise in $http.get
            return $http.get('/d/GetDealerLocationTimes?website_id=' + websiteId)
                .then(function(response) {
                    // dealerLocationTimeList.push(response.data);
                    return response.data; // return the data for the resolve part will make it available when the outer promise resolve
                }/* this whole part should be omitted if we are not doing any processing to error before returning it (thanks @Bergi)
                , function(error) {
                    return $q.reject(error); // use $q.reject to make this available in the reject handler of outer promise
                }*/);

            // no need to return promise here anymore
        }
    }
}]);
promise.then(
    function(da) {
        // you might want to do an isArray check here, or make sure it is an array all the time
        $scope.dealerLocationTimeList.push(da);
        console.log($scope.dealerLocationTimeList);
    },
    ...
);