Suraz Khanal Suraz Khanal - 29 days ago 8
AngularJS Question

How to call function in angularjs factory

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.

Answer

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.

Working Example - http://plnkr.co/edit/y8WZQT8SvOAWpKj8Jgxs?p=preview

Comments