恕我直言,最好的方法是将每个请求分成单个服务方法,这样您就可以仅重用其中的一部分而不是通过服务器调用来加载整个数据,请检查 angular-resource $resource 以获得干净的可重用服务器调用服务而不是一堆 $https arround 您的代码:
示例:
指向后端服务器的一些 url 的服务
.factory('ClientService', ['$resource', function($resource){
return $resource('http://some_url/:controller/:method', null, {
"agents": { method: 'GET', params: { controller: 'agent', method: 'search' }, cache: false },
"query": { method: 'GET', params: { controller: 'client', method: 'search' }, cache: false },
"save": { method: 'POST', params: { controller: 'client', method: 'save' } },
"delete": { method: 'POST', params: { controller: 'client', method: 'delete' } }
})
}])
在控制器中的使用(注入ClientService作为依赖)
// If i want to query the agents into a scope element
// that will call the url = http://some_url/agent/search
$scope.agents = ClientService.agents();
// If i want to query a single client i cant send adtional params
// as is a get request it will call http://some_url/client/search?id=5
$scope.client = ClientService.query({id:5});
// and you can event manage callbacks if you want to
// This will send the client object to the url = http://some_url/client/save
ClientService.save($scope.client).$promise.then(function(response){ alert(response) })
如您所见,您可以从后端服务器仅访问您需要的内容,如果您不需要,则无需执行所有回调响应,并且以可重复使用的更简洁方式
信息Angular Resource Docs