0

我不明白为什么我的函数没有设置我的全局变量。我的代码:

 var localizeRegForm = {};


 var handlerLocalDef = function(defer) {
     var hash;

     defer.then(
         function(response) {
             return hash = response.data;
         },
         function(err) {
             showPopup(err);
         }
      );

      return hash;
  };

  var initialized = function() {
      console.log("localizeRegForm",localizeRegForm); 
      localizeRegForm = handlerLocalDef(Localization.getLocalizedDefer('regularform'));
      console.log("localizeRegForm",localizeRegForm)
  }

我的控制台显示:

  1. localizeRegForm Object {}
  2. localizeRegForm undefined
4

2 回答 2

0

像这样使用它

    var deferred = $q.defer();
        $http({
            method: 'POST',
            url: 'something',
            data: data
        }).
        success(function(response, status, headers, config) {
            deferred.resolve(response);
        }).
        error(function(response, status, headers, config) {
            deferred.reject("");
        })
        return deferred.promise;
于 2013-09-26T08:43:41.230 回答
0

最好重写它:

        var initialized = function()  { 
                Localization.getLocalizedDefer('regularform').then(function(response){
                    localizeRegForm = response.data;
                    console.log("localizeRegForm",localizeRegForm)
                });
             }

问题不在于 AngularJS,更多的是关于使用延迟对象

于 2013-09-26T08:42:45.917 回答