admin管理员组文章数量:1334697
this is my factory and i want to call getData in saveData.Here is my code
.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.
this is my factory and i want to call getData in saveData.Here is my code
.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.
Share Improve this question asked Nov 5, 2016 at 8:34 Suraz KhanalSuraz Khanal 2221 gold badge5 silver badges17 bronze badges2 Answers
Reset to default 6You 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.
Working Example - 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();
}
}
})
You 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.
本文标签: javascriptHow to call function in angularjs factoryStack Overflow
版权声明:本文标题:javascript - How to call function in angularjs factory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365609a2461196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论