在您的服务或控制器中处理$ http承诺?

时间:2015-09-02 08:32:05

标签: angularjs

我发现我的控制器充满了承诺功能,而我的服务只有几行,

是处理服务承诺的好习惯吗?

如下图所示,我使用$ http.post,我的服务中还有更多$ http.get。

// $http.post
    logService.insertLog($scope.newLog)
        .then(onInsertSuccess)
        .catch(onError);
// $http.get
    logService.getPriority()
        .then(onPrioritySuccess)
        .catch(onError);

var onPrioritySuccess = function (response) {
    $scope.priority = response.data;
};

这样的东西会更好吗?

// store the response in the scope directly?
$scope.priority = logService.getPriority();

1 个答案:

答案 0 :(得分:0)

无论如何,因为getPriority最终是$ http请求,所以你必须返回一个promise,这样你的控制器代码看起来就像了。

你在顶部写的内容似乎很好。