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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

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

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