【问题标题】:How to call function in angularjs factory如何在angularjs工厂中调用函数
【发布时间】:2023-03-26 00:31:01
【问题描述】:

这是我的工厂,我想在 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 中。

【问题讨论】:

    标签: javascript angularjs angular-promise


    【解决方案1】:

    你总是可以做的,像这样 -

    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();
          }
      }
    })
    

    【讨论】:

      【解决方案2】:

      您不必在返回的对象字面量中声明所有函数。你可以这样做:

      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
      
            }
      });
      

      甚至更喜欢这种方式。请关注angular's style guide

      【讨论】:

        猜你喜欢
        • 2014-11-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        • 1970-01-01
        相关资源
        最近更新 更多