我們使用的是古老的 (1.x) 版本的 AngularJS。我正在嘗試使用承諾的結果 - 但我似乎無法獲得價值。在我在這里提供的示例中 - 您可以看到我什至嘗試使用 global / window 范圍來存盤并稍后檢索該值。
在以前的版本中,我嘗試了var xxx和$scope.xxx,結果相同:我似乎無法訪問我的承諾的內容。
我的代碼;
angular.module('surveyBuilder').controller('cloneUsersCtrl', cloneUsersCtrlFunction);
cloneUsersCtrlFunction.$inject = ['$http','$scope', '$state', '$stateParams', '$cookies', 'TenantHandler', 'Survey', 'TENANT', 'PERMISSION', 'Notification', '$log'];
function cloneUsersCtrlFunction($http, $scope, $state, $stateParams, $cookies, TenantHandler, Survey, TENANT, PERMISSION, Notification, $log) {
$scope.surveyId = $stateParams.surveyId;
$scope.selectedTenant = '' TenantHandler.getSelected();
$scope.canEdit = PERMISSION.edit;
$scope.activeOnly = false;
var getDatasources = function () {
return $http.get(apiBase 'tenants/' $scope.selectedTenant '/surveys/' $scope.surveyId '/cloneUsers');
}
getDatasources().then(function(data) {
console.log('data.data: line 439: ' data.data);
// Use the global WINDOW scope
window.gavinData = data.data;
console.log('window.gavinData: Line 442: ' window.gavinData);
});
console.log(window);
console.log('window.gavinData: Line 445:' window.gavinData);
這是控制臺的輸出。
app.js:444 Window {... //clipped for succintness
app.js:445 window.gavinData: Line 445:undefined
app.js:439 data.data: line 439: 362,CUSTOMERFIRSTBANK daily email recipient data,363,Retail Banking Survey,371,Medibank Survey,383,Copy of Frequent Flyer Card Survey,404,DeplyTest,405,Petbarn Survey,406,Copy of Petbarn Survey
app.js:442 window.gavinData: Line 442: 362,CUSTOMERFIRSTBANK daily email recipient data,363,Retail Banking Survey,371,Medibank Survey,383,Copy of Frequent Flyer Card Survey,404,DeplyTest,405,Petbarn Survey,406,Copy of Petbarn Survey
在 Chrome 的開發者工具中,如果我展開Window物件,我可以看到我的資料的節點;
gavinData: (7) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
那么為什么它在第 445 行未定義?我需要做什么才能訪問嵌套陣列的內容?
謝謝!
uj5u.com熱心網友回復:
根據 promise 的性質,您不知道它何時解決(= 部件何時then運行)。
首先,getDatasources()被執行,然后在沒有人知道的某個時間之后,它解決了。同時這些行低于then塊
console.log(window);
console.log('window.gavinData: Line 445:' window.gavinData);
被執行。此時,promise 可能已經解決,也可能沒有。
uj5u.com熱心網友回復:
我已經設法解決了。我的想法全錯了。
我掉進了一個陷阱,認為因為我有一個.then- 和.then呼叫后的代碼- 它使它自動可用于以后的代碼 - 這顯然是錯誤的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/366875.html
