这是我的工厂,我想在saveData中调用getData。这是我的代码
.factory('dataSyncOperation', function($q,$http){
return {
getData:function(){
var q = $q.defer();
var config = {
headers : {
'Content-Type': 'application/json'
}
}
$http.get(api+'/sync/').then(function(response){
q.resolve(response);
},function(error){
q.reject();
})
return q.promise;
},
saveData:function(){
}
}
}); 我如何使用getData返回的promise到saveData中。
您可以随时这样做,就像这样-
saveData:function(){
this.getData().then(function(response){ // you can handle getData promise here
// on success
}, function(reject){
// on failure
});
}
在您的saveData
方法中,让我知道这是否是您想要的东西。
工作示例-http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview
代码 -
// Code goes here
var myApp = angular.module('myApp', []);
myApp.controller('mainCtrl', function($scope, testService){
testService.saveData().then(function(res){
$scope.test = res.data;
});
})
myApp.factory('testService', function($q, $http){
return {
getData:function(){
var q = $q.defer();
$http.get('data.json').then(function(response){
q.resolve(response);
}, function(error){
q.reject();
})
return q.promise;
},
saveData:function(){
return this.getData();
}
}
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句