【发布时间】:2014-10-29 06:18:11
【问题描述】:
我的印象是,所有与数据相关的请求都必须委托给服务/工厂。但是阅读 $http 实现的示例met 不是一次将它们放在控制器内部,例如:
var app = angular.module("MyApp", []);
app.controller("PostsCtrl", function($scope, $http) {
$http.get('data/posts.json').
success(function(data, status, headers, config) {
$scope.posts = data;
}).
error(function(data, status, headers, config) {
// log error
});
});
这被认为是正常的做法/模式还是只是为了举例?
【问题讨论】:
-
作为一种好的设计模式,最好在服务/工厂中使用 $http。使用 $resource 而不是 $http
-
这里只是示例如何使用 $http。但是对于编码,大多数人在服务中使用每个逻辑事务、检索等。控制器应该只作为视图和服务之间的信使。
-
甜蜜!谢谢,伙计们!
标签: javascript angularjs oop design-patterns