get the value of $scope.data in a variable in same controlled

别等时光非礼了梦想. 提交于 2019-12-13 11:07:15

问题


My code is some thing

        $scope.data = {};
        var init = function () {
            $http.post('views/Report.aspx/GetMailReport', {})
            .success(function (data, status, headers, config) {
                $scope.data = JSON.parse(data.d);
            })
            .error(function (data, status, headers, config) {
                $scope.data = status;
            })
        };
        init();

        var data = $scope.data;

But the var data is returning empty {}


回答1:


If you want a copy you should do as below... As the http call is asynchronous you assign inside the success callback method

$scope.data = {}; 
var data;

var init = function () {           
  $http.post('views/Report.aspx/GetMailReport', {}) .success(function (data, status, headers, config) {
  $scope.data = JSON.parse(data.d); 
data = $scope.data;
}) .error(function (data, status, headers, config) { 
  $scope.data = status; 
}) }; 
init(); 

Edited: Here is the link to a fiddle that I have created.



来源:https://stackoverflow.com/questions/26572610/get-the-value-of-scope-data-in-a-variable-in-same-controlled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!