是否可以将GET响应数据传递回同一工厂
Is it possible to pass GET response data back to same factory?
问题:
从任何控制器,如何调用 getPages
函数,将数据返回给控制器,并将空的 Page.details.ref
对象替换为GET
响应数据?
无论哪个控制器调用函数,这一切都有可能在工厂内发生吗?
app.factory('Pages', function($http, ENV){
var Pages = {};
Pages.details =
{
pages:
{
length: 0,
offsets: []
},
ref:
{
//data goes here on success
},
getPages: function($scope) {
return $http.get(ENV.apiEndpoint + '/' + $scope.storeSlug + '/pages.json?code=' + $scope.promoCode)
.success(function(data){
// I want this Pages.details.ref to be replaced on success of getPages
Pages.details.ref = data;
$scope.handlePagesSuccess(data);
return data;
})
.error(function(data, status){
// console.log('error:' + status);
});
}
}
return Pages;
});
控制器:此控制器调用初始化请求
app.controller('RandomCtrl', function($scope, Pages){
var handleSuccess = function (data) {
$scope.data = data;
}
Pages.details.getPages($scope).success(handleSuccess);
})
控制器 #2:此控制器仅使用临时版本的请求,RandomCtrl之间没有关系。 例如,此控制器通常是一个指令级控制器,其中父 ctrl 之间没有冒泡
app.controller('OtherCtrl', function($scope, Pages){
$scope.tempPage = Pages.details.ref;
})
从哪里调用getPages应该无关紧要。 我希望每次调用getPages时都替换ref。
您似乎正在尝试管理工厂内部的状态,这可能不是一个好主意。在工厂里传递$scope
也不是一个好主意。它们应仅限于其自己的控制器。您可以改为缓存上一次调用的承诺,并且基于标志,您可以返回缓存的承诺或进行实际的服务调用。
app.factory('Pages', function($http, ENV, $q){
var Pages = {};
var cachedPromise = {};
Pages.details =
{
pages:
{
length: 0,
offsets: []
},
getPages: function(request) {
//Get a request key to make sure you are returning right promise incase multiple product calls are made at the same time.
var reqKey = request.storeSlug + request.promoCode;
//if a call has already been made and there is a promise return it
if(cachedPromise[reqKey]) return cachedPromise[reqKey];
//Store the promise in the cache for lastCall retrieval
return cachedPromise[reqKey] = $http.get(ENV.apiEndpoint + '/' + request.storeSlug + '/pages.json?code=' + request.promoCode)
.then(function(result){
return result.data; //You can alter data and send as well
}, function(data, status){
return $q.reject('some error'); //or return some data
}).finally(function(){
//remove the cache from the map, once promise is resolved.
delete cachedPromise[reqKey];
});
}
}
return Pages;
});
在您的第一个控制器中,请:-
app.controller('RandomCtrl', function($scope, Pages){
//Build your request.
Pages.details.getPages(request).then(function (data) {
$scope.data = data;
});
});
在您的第二个控制器中,只需执行相同的操作:-
app.controller('OtherCtrl', function($scope, Pages){
//Pass the flag to get the cached data.
Pages.details.getPages(request).then(function (data) {
$scope.tempPage = data;
});
});
相关文章:
- 如何在具有某些 UI 视图的状态中管理工厂单个请求数据
- 使用2个控制器管理工厂JSON$http数据
- 为什么针对工厂的Angular数据绑定只适用于函数
- 将工厂服务数据发送到控制器,以便在视图中使用
- 从 Angular 数据工厂中的 HTTP POST 请求接收未定义的结果
- 访问 angularjs 工厂数据之外的承诺
- 如何将数据从 angularJS 工厂填充到控制器
- 使用工厂/服务在控制器AngularJS之间推送数组中的数据
- 工厂返回数据后未设置角度$scope
- 如何在angular js中将数据从工厂传递到控制器
- Angularjs-向工厂传递数据的方法不起作用
- 从角度工厂返回数据,返回对象后填充数据
- 从控制器将数据发送到工厂
- 数据不充当服务的全局表单工厂
- 从父控制器传递工厂数据以隔离范围指令
- 角度服务/工厂配置数据
- 控制器未从工厂返回数据
- 使用 AngularJS 工厂加载 json 数据
- 角度工厂获取数据
- 自定义指令控制器数据与工厂数据同步