【问题标题】:Manage Success and Error responses from services管理来自服务的成功和错误响应
【发布时间】:2015-02-19 08:21:38
【问题描述】:

在我的控制器上,我有大量的服务调用,所以每次我得到响应时,我都在做同样的操作,所以我想知道是否有办法统一“成功" 和 "error" 功能用于每个服务调用,以便删除、重复的代码,

另外,如果有办法应该放在哪里?

 this.UpdateProfile = function (profile) {

                profilePageDataService.UpdateProfile(profile)
                    .success(
                        function (data, status) {
                          alert('Yeah');                                    
                        })
                    .error(
                        function (data, status) {                          
                           alert('Ko');    
                        });
     };

编辑:

所以,我想澄清一下,我只想取出“成功和错误”,因为里面的代码是一样的,

this.UpdateProfileName = function (profile) {

                profilePageDataService.UpdateProfileName(profile)
                    .success(
                        function (data, status) {        
                           alert('Yeah');              
                        })
                    .error(
                        function (data, status) {                          
                            alert('Ko'); 
                        });
     };

【问题讨论】:

    标签: javascript angularjs angular-services


    【解决方案1】:

    您可以为服务函数添加回调。

    this.UpdateProfile = function (profile, callback) {
    
        var cb = callback || angular.noop; // just a safer way. do nothing in you did not specify `callback` variable.
    
        profilePageDataService.UpdateProfile(profile)
            .success(
                function (data, status) {
                    return cb(data);
            })
            .error(
                function (data, status) {                          
                    return cb(data);
            });
    };
    

    并获得回调:

    var userProfile = {};
    
    MyService.UpdateProfile(userProfile, function(response) {
        console.log(response);
    });
    

    【讨论】:

      【解决方案2】:

      如何在 $rootScope 中移动您的 UpdateProfile 函数:

      angular.module('scopeExample', [])
      .controller('RootController', ['$scope', '$rootScope', 'profilePageDataService', function($scope, $rootScope, profilePageDataService) {
      
        $rootScope.UpdateProfile = function (profile) {
      
                      profilePageDataService.UpdateProfile(profile)
                          .success(
                              function (data, status) {
      
                                //Some operations here...
      
                              })
                          .error(
                              function (data, status) {                          
                                //.....
                              });
           };
      }])
      .controller('ProfileController', ['$scope', function($scope) {
         //UpdateProfile function moved from here to $rootScope
      }]);
      

      我认为上面可以工作(注意我没有测试过),但上面的代码是在Angular's Scope Hierarchies 示例之后实现的。

      在这种情况下还有另一种可能也可以使用,它涉及使用AngularUI Router

      【讨论】:

        猜你喜欢
        • 2019-05-02
        • 2018-06-20
        • 2020-09-10
        • 1970-01-01
        • 2017-05-25
        • 2015-10-10
        • 2012-12-09
        • 2023-03-17
        • 2012-04-08
        相关资源
        最近更新 更多