【问题标题】:AngularJS factory function add promiseAngularJS工厂函数添加承诺
【发布时间】:2018-01-09 12:40:26
【问题描述】:

我正在 Angular 工厂中创建一个函数。我需要这个函数返回承诺。然后我添加其他功能。

    SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope) {
        function initialize($scope, status) {


    $http({
        method: 'GET',
        url: '/api/ApiCustomer/GetCustomerDetails'

    }).then(function (data) {
        if (data.data.ModelValue == null) {
            $rootScope.redirectToLogin();
        } else {
            $scope.CustomerInformation = data.data.ModelValue;


        }
    }).then(function () {
            $http({
                method: 'GET',
                url: '/api/ApiList/AccountTypeList'
                }).success(function (result) {
                $scope.accountTypeList = result;
                typeOfAccount = result;
            });

            // get currncy list

            $http({
                method: 'GET',
                url: '/api/ApiList/CurrencyList'
                }).success(function (result) {
                $scope.currencyList = result;

                //jQuery('#ProfileImage').attr('src', "/Content/Images/blank-avatar.jpg");
            });


            $http({
                method: 'GET',
                url: '/api/ApiList/BankListByUserType',
                }).success(function (result) {
                $scope.bankList = result;
            });

    });

};

return {
    initialize: initialize
}
    }]);

以及如何调用这个函数作为一个promise然后使用

processing.initialize($scope, true).then(function(){
  console.log("hello world");
});

如果你知道这个过程,那么现在不起作用请帮助

【问题讨论】:

    标签: javascript angularjs angular-promise


    【解决方案1】:

    你应该从工厂返回Promise

    SPApp.factory('processing', ['$http', '$rootScope', '$q',   function ($http, $rootScope, $q) {
            function initialize($scope, status) {
                return $http({
                    method: 'GET',
                    url: '/api/ApiCustomer/GetCustomerDetails'
                });
            }
    
            return {
                initialize: initialize
            }
        }
    ]);
    

    在控制器方法中使用输出

    processing.initialize($scope, true).then(function (data) {
        $scope.basicInfo = data.data.ModelValue.PersonalInformations[0];
    });
    

    根据评论 initialize 函数有多个 http 请求您可以使用 $q.all() 方法,该方法将等待所有承诺完成。

    SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
            function initialize($scope, status) {
                var returnData = {},
                var a = $http({
                        method: 'GET',
                        url: '/api/ApiCustomer/GetCustomerDetails'
                    }).then(function (response) {
                        returnData.basicInfo = response.data.ModelValue.PersonalInformations[0];
                    });
    
                var b = $http({
                        method: 'GET',
                        url: '/api/ApiList/AccountTypeList'
                    }).then(function (response) {
                        returnData.accountTypeList = response;
                    });
    
                return $q.all([a, b]).then(function () {
                    return returnData;
                });
            }
    
            return {
                initialize: initialize
            }
        }
    ]);
    

    【讨论】:

      【解决方案2】:

      查看以下链接以了解 Angular 中的 Promise 函数,这有助于解决此问题

      https://docs.angularjs.org/api/ng/service/$q

      【讨论】:

        【解决方案3】:

        你应该

        1. return 承诺而不是仅仅执行它
        2. 使用承诺链并在工厂外部处理接收到的数据,而不是在内部传递$scope
        3. 另外,$q 服务似乎未使用(不确定您的计划)。

        工厂代码

            SPApp.factory('processing', ['$http', '$rootScope', function ($http, $rootScope) {
            function initialize(status) {
                return $http({
                    method: 'GET',
                    url: '/api/ApiCustomer/GetCustomerDetails'
        
                }).then(function (data) {
                    return data.data.ModelValue.PersonalInformations[0];
                }
            }
        
            return {
                initialize: initialize
            }
        }]);
        

        用法

        processing.initialize(true).then(function(personalInformation){
          console.log(personalInformation);
        });
        

        【讨论】:

          猜你喜欢
          • 2017-01-30
          • 1970-01-01
          • 1970-01-01
          • 2014-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-12
          相关资源
          最近更新 更多