2017-01-11 27 views
0

我需要將$http.get返回的數據存儲在$scope變量中,以供我的ui-grid控件使用。

我確定我錯過了一些非常簡單的事情,但我無法找出什麼。這裏是我的代碼:

app.controller('AdminUsersGridCtrl', function ($scope, $http, uiGridConstants) { 

    $http.get('/api/admin/user') 
    .success(function (response, $scope) { 
     $scope.myData = response; 
    }) 
    .error(function (error) { 
     console.log(error); 
    }); 

    console.log($scope.myData); 

    $scope.gridOptions = { 
     data: 'myData', 
     enableFiltering: true, 
     columnDefs: [ 
     { field: 'firstName' }, 
     { field: 'lastName' }, 
     { field: 'jobTitle'}, 
     { 
      field: 'email', 
      filter: { 
      condition: uiGridConstants.filter.ENDS_WITH, 
      placeholder: 'ends with' 
      } 
     }, 
     { 
      field: 'phone', 
      filter: { 
      condition: function(searchTerm, cellValue) { 
       var strippedValue = (cellValue + '').replace(/[^\d]/g, ''); 
       return strippedValue.indexOf(searchTerm) >= 0; 
      } 
      } 
     }, 
     ] 
    };   

    }); 

我的控制檯成功打印undefined。如何訪問$http.get success功能中的原始$scope

+3

您可以訪問它,只是因爲AJAX =異步,您的代碼的其餘部分在「成功」之前執行。當您的控制檯記錄myData範圍變量時,將代碼移動到回調 – tymeJV

+1

,確實是** undefined **,因爲您仍在執行**異步http獲取請求**。如果你需要將「myData」綁定到gridOptions,並且希望100%確定它將被正確加載並且沒有錯誤,請在**。success **回調中解析代碼,或者創建一個'Promise'並解析它一旦http請求完成。 – briosheje

回答

1
My console inside success prints undefined 

但是,您的控制檯不在成功方法內。這裏面:

$http.get('/api/admin/user') 
.success(function (response) { 
    $scope.myData = response; 
    console.log($scope.myData); 
}) 
.error(function (error) { 
    console.log(error); 
}); 

這不是:

// order of events.. 
// #1...first $http 
$http.get('/api/admin/user') 
.success(function (response) { 
    //... #3 third. Now finally response is defined. the end 
    $scope.myData = response; 
}) 
.error(function (error) { 
    console.log(error); 
}); 

//... #2 second. $scope.myData definitely IS undefined 
console.log($scope.myData); 

巨大差異。在您的成功回調中也不需要包含$scope。我以前從來沒有見過。你從哪裏得到那個的?

+0

明顯。謝謝。但是我將在成功函數中訪問的$ scope將是原始的(閉包)? – Mendes

+0

沒關係。 Angular使用摘要循環來更新異步事件的視圖,其中'$ http'恰好是 –

+0

之一如果您正在根據角度不知道的事件(不是常見用例)更改範圍,則必須擔心這一點,並手動運行摘要循環。 –

0

如果您在外部文件中使用服務時服務器調用更好,但在您的情況下,$ scope沒有響應,因爲代碼在服務完成之前執行,請嘗試使用$超時或像這樣:

app.controller('AdminUsersGridCtrl', function ($scope, $http, uiGridConstants) { 

$http.get('/api/admin/user').success(function (response) { 
    $scope.myData = response; 
    fillGridOptions(); 
}).error(function (error) { 
    console.log(error); 
}); 

function fillGridOptions() { 
$scope.gridOptions = { 
    data: 'myData', 
    enableFiltering: true, 
    columnDefs: [ 
     { field: 'firstName' }, 
     { field: 'lastName' }, 
     { field: 'jobTitle' }, 
     { 
      field: 'email', 
      filter: { 
       condition: uiGridConstants.filter.ENDS_WITH, 
       placeholder: 'ends with' 
      } 
     }, 
     { 
      field: 'phone', 
      filter: { 
       condition: function (searchTerm, cellValue) { 
        var strippedValue = (cellValue + '').replace(/[^\d]/g, ''); 
        return strippedValue.indexOf(searchTerm) >= 0; 
       } 
      } 
     }, 
    ] 
}; 

}

});