目前我在三个控制器上都有这样的电话:
$scope.getCurrentUser = function () {
$http.post("/Account/CurrentUser", {}, postOptions)
.then(function(data) {
var result = angular.fromJson(data.data);
if (result != null) {
$scope.currentUser = result.id;
}
},
function(data) {
alert("Browser failed to get current user.");
});
};
我看到很多建议将$http
调用封装到HttpService或其他类似的东西中,但是返回promise比返回数据要好得多。然而,如果我返回承诺,我的控制器$http
中除了一行之外的所有行都会调用更改,并且处理响应的所有逻辑都保留在我的控制器中,例如:
$scope.getCurrentUser = function() {
RestService.post("/Account/CurrentUser", {}, postOptions)
.then(function(data) {
var result = angular.fromJson(data.data);
if (result != null) {
$scope.currentUser = result.id;
}
},
function(data) {
alert("Browser failed to get current user.");
});
};
我可以为每个服务器端控制器创建一个RestService,但这最终只能调用核心服务并传递URL。
答案 0 :(得分:2)
控制器执行表示逻辑(它在Angular Model-View-Whatever模式中充当视图模型)。服务做业务逻辑(模型)。这是经过战争验证的关注点分离和OOP良好实践的固有部分。
精简控制器和胖服务可确保应用程序单元保持可重用,可测试和可维护。
如果将$http
替换为RestService
,如果它们是相同的,则没有任何好处。业务和表示逻辑的正确分离应该是这样的
$scope.getCurrentUser = function() {
return UserService.getCurrent()
.then(function(user) {
$scope.currentUser = user.id;
})
.catch(function(err) {
alert("Browser failed to get current user.");
throw err;
});
});
它负责结果调节并返回一个承诺。 getCurrentUser
传递一个promise,因此可以根据需要链接(通过其他控制器方法或测试)。
答案 1 :(得分:2)
有几个原因可以解释为什么它是非平凡应用程序的良好实践。
使用单个通用服务并传入url和参数并不会增加您注意到的价值。相反,您需要为每种类型的提取设置一种方法。
使用服务的一些好处:
答案 2 :(得分:0)
让您的服务看起来像这样:
app.factory('AccountService', function($http) {
return {
getCurrentUser: function(param1, param2) {
var postOptions = {}; // build the postOptions based on params here
return $http.post("/Account/CurrentUser", {}, postOptions)
.then(function(response) {
// do some common processing here
});
}
};
});
然后调用此方法将是这样的:
$scope.getCurrentUser = function() {
AccountService.getCurrentUser(param1, param2)
.then(function(currentUser){
// do your stuff here
});
};
它看起来更好,可以避免在多个控制器中重复后端服务URL和postOptions
变量构造。
答案 3 :(得分:0)
简单。将每个函数写为服务,以便您可以重用它。由于这是一个异步调用,因此使用角度承诺通过将数据包装在promise中来将数据发送回控制器。