2015-07-20 124 views
0

我需要在下面的函数中打印出localeData值。 我知道如何在功能方面打印,就像我在这里所做的一样。 我试过这个,但没有工作。Angularjs获取函数外的对象值

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     console.log(localeData); 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 
     //need to get that value here. 
     console.log(localeData); 

编辑

其实我需要做的就是这个

app.factory('customLoader', function($http, $q, SERVER_URL) { 

    return function(options) { 
     var deferred = $q.defer(); 

     // do something with $http, $q and key to load localization files 
     $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     //do something here to localData 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 

     deferred.resolve(localeData); 
     return deferred.promise; 

    }; 
}); 

这正是我需要的。最后我需要发送localeData。

+0

你应该把这个输出分配给控制器的对象/变量 – Vineet

+0

任何想法该怎么做? – Mad

+0

你为什么要在功能外打印? –

回答

0

你不需要$ Q来实现自己的目标:

app.factory('customLoader', ["$http", "SERVER_URL", function($http, SERVER_URL) { 
    return function(options) { 
     return $http.post(SERVER_URL + 'getLocaleData'); 
    }; 
}]); 

因为$http和所有它的方便的方法实际上是返回一个承诺。在这种情况下,你可以按如下方式使用该服务:

customLoader(options).success(function(localeData){ 
    //do something here to localData 
}).error(function(err){ 
    alert('warning', err.message); 
}); 

如果你真的必须使用$q,那么这应该这样做:

app.factory('customLoader', ["$http", "$q", "SERVER_URL", function($http, $q, SERVER_URL) { 
    return function(options) { 
     return $q(function(resolve, reject){ 
      $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
       //do something here to localData 
       resolve(localeData); // ultimately you ought to resolve inside this function 
      }).error(function(err) { 
       alert('warning', err.message); 
       reject(localeData); // ultimately you ought to reject inside this function 
      }); 
     }); 
    }; 
}]); 
+0

我得到了一个下面的错误。 TypeError:$ q不是功能 \t return $ q(function(resolve,reject){ – Mad

+0

好的,这是不太可能的。你如何使用这个服务?同时我对我提供的示例做了一个非常小的更新。 –

+0

不幸的是$ q是不是一个函数。 你肯定是它的功能? 我只是复制粘贴代码。 – Mad

0

JS是异步的,这基本上意味着你的最后一个console.log将在服务器返回任何数据之前执行。所以,你必须这样做既对您的承诺中:

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    console.log(localeData); 
    }).error(function(err) { 
     console.log(err); 
     alert('warning', err.message); //Just in case you didn't know, this line supposes that your server returns a JSON object with a "message"-property 

    }); 

这样一来,你的服务器响应将是成功和失败console.log“版两种。

Reference

编辑:如果你真的想这样做的另一种方式:

var response; //First, declare a variable in the parent scope. 

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    response = localeData; //Assign the response data to the variable. 
    console.log(localeData); 
    }).error(function(err) { 
     alert('warning', err.message); //Just in case you didn't know, this line supposes that your server returns a JSON object with a "message"-property 

    }); 
    console.log(response); 

如您所愿的例子不会评价,虽然。最后一个console.log将只记录undefined

+0

您也可以使用'q'服务。 – Vineet

+0

我知道如何做到这一点的功能。 – Mad

+0

你必须了解JS的体系结构。即使你试图在功能之外访问,这也是反模式的标志。尽管如此,我会更新我的答案,试着让你知道如何去做。 – Dencker

0

localeData仅限于.success函数的范围。除非你指定其他变量,否则你不能在外面得到它。

var data = "" 
$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    data = localeData; 
    }).error(function(err) { 
     alert('warning', err.message); 
    }); 
//need to get that value here. 
console.log(data); 

但是不要指望在data之前得到想要的值,直到得到回应为止。 $http.post异步

看看承诺克服这样的问题,因为它们使用的所有的地方,在世界的角度。

UPDATE:

当您在编辑所做的那样,可以选择使用$ Q服务,如:

app.factory('customLoader', function($http, $q, SERVER_URL) { 

    return function(options) { 
     var deferred = $q.defer(); 

     // do something with $http, $q and key to load localization files 
     $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     //do something here to localData 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 

     deferred.resolve(localeData); 
     return deferred.promise; 

    }; 
}); 

或者,你可以简单地返回$http.post这本身会返回一个承诺

app.factory('customLoader', function($http, $q, SERVER_URL) { 
    return function(options) {   
     return $http.post(SERVER_URL + 'getLocaleData'); 
    }; 
}); 

无论哪种方式,您将返回一个承诺对象,而不仅仅是从服务器获得的实际响应。该localeData可以如下访问:

customLoader().success(function(res){ 
    console.log(res); 
}).error(function(){ 
    //show some error message 
}) 
0

通过以下方式,您可以访问您的服务器响应控制器外部工厂:

app.factory('customLoader', function ($http, $q) { 
     return { 
      response: {}, 
      getLocalData: function (param) { 
       var deferred = $q.defer(); 
       $http.post(SERVER_URL + 'getLocaleData', param). 
        success(function (data, status, headers, config) { 
         deferred.resolve(data); 
        }). 
        error(function (data, status, headers, config) { 

        }); 

       return deferred.promise; 
      } 
     } 
    }); 


app.controller('yourCtrl', function($scope,customLoader) { 

     $scope.GetLocaleData= function() {  
       customLoader.getLocalData({ 
        Role: role, 
        Email_Id: $scope.Email, 
        Pwd: $scope.Password 
       }).then(function(localeData) { 
        $scope.Data = localeData;       
       });   
     } 
}); 
1

您无需创建&解决你自己。有一个更简单的方法。 (同样在你给的例子中,你解决承诺之前,基础阿贾克斯完成)

Angular.js $http服务接口$q接口呢!所以你可以写:

app.factory('customLoader', function($http, $q, SERVER_URL) { 
    return function(options) { 
    var promise = $http.post(SERVER_URL + 'getLocaleData') 
     .then(function(localeDataBag) { 
     var localeData = localeDataBag.data; // .data of 'then' equals to what .success returns.    
     modifiedLocaleData = localeData++; // do something here to localData 
     return modifiedLocaleData; // This is the new result 
     }) 
     .catch(function(err) { 
     console.log(err); 
     return $q.reject(err); // This is the new result if something failed. Since we are chaining promises, we should keep failure state. 
     }); 
    return promise; 
    }; 
});