this is my factory and i want to call getData in saveData.Here is my code
这是我的工厂,我想在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(){
}
}
}); How can i use the promise returned by getData into saveData.
});我怎样才能将getData返回的promise用于saveData。
2 个解决方案
#1
3
You can always do, something like this -
你可以随时做,像这样 -
saveData:function(){
this.getData().then(function(response){ // you can handle getData promise here
// on success
}, function(reject){
// on failure
});
}
inside your saveData
method, let me know if this is what something you are looking for.
在你的saveData方法中,让我知道这是你正在寻找的东西。
Working Example - http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview
工作示例 - http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview
Code -
代码 -
// 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();
}
}
})
#2
1
I don't have to declare all functions in object literal being returned. You can do something like this:
我不必声明返回对象文字中的所有函数。你可以这样做:
factory('dataSyncOperation', function($q,$http){
function getData(){ //you can declare function inside function and it will be avaible only inside scope of outer 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;
}
getData(); //call get data
function saveData() {
myPrivateFunction();
getData(); //call get data inside save data
}
function myPrivateFunction(){ //you can even have private functions not avaible from outside
}
return { //declare which functions will be available from outside
getData:getData,
saveData:saveData
}
});
This way is even preffered. Please have a look on angular's style guide.
这种方式甚至是优先考虑的。请看一下棱角分明的风格指南。
#1
3
You can always do, something like this -
你可以随时做,像这样 -
saveData:function(){
this.getData().then(function(response){ // you can handle getData promise here
// on success
}, function(reject){
// on failure
});
}
inside your saveData
method, let me know if this is what something you are looking for.
在你的saveData方法中,让我知道这是你正在寻找的东西。
Working Example - http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview
工作示例 - http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview
Code -
代码 -
// 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();
}
}
})
#2
1
I don't have to declare all functions in object literal being returned. You can do something like this:
我不必声明返回对象文字中的所有函数。你可以这样做:
factory('dataSyncOperation', function($q,$http){
function getData(){ //you can declare function inside function and it will be avaible only inside scope of outer 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;
}
getData(); //call get data
function saveData() {
myPrivateFunction();
getData(); //call get data inside save data
}
function myPrivateFunction(){ //you can even have private functions not avaible from outside
}
return { //declare which functions will be available from outside
getData:getData,
saveData:saveData
}
});
This way is even preffered. Please have a look on angular's style guide.
这种方式甚至是优先考虑的。请看一下棱角分明的风格指南。