1

我試圖通過其我從JavaScript文件數組獲取值,並通過使用一個全局變量如何從javascript傳遞數組值angularjs控制器

在角文件它推到angularJS,我試圖通過訪問像 全局變量來獲取這些值,但它返回我不確定,但是我跑了

我怎麼能在我的控制研究訪問此值

+3

如果變量是全局變量(我的意思是窗口級全局變量),那麼它在您的應用程序中的任何位置都可用,包括您的控制器內部。 –

+1

首先,abc函數聲明是錯誤的,你在{ – Kalamarico

+2

@ v.josh之前忘記了()你可以發佈[** Minimal,Complete和Verifiable示例**](https://stackoverflow.com/help/mcve )?既然這樣'var globalarray = [];函數abc(){var array1 = [4,5]; globalarray.push(數組1)}; ABC(); (function(){console.log(globalarray [0]);})();'會給出正確的輸出:'[4,5]'。 –

回答

-2

全局變量是不是一個好主意,也許你需要與其他互動網頁或框架。

是你應該烏斯一個全局變量,你可以在index.html的一個特殊的腳本聲明

<script type="text/javascript">  
let globalarray; 
</script> 

你也可以ASIGN的globalarray到窗口的屬性

window.globalarray 

或者$ rootScope。

+0

我已經試過這一個$ scope.globalarray = $ window.globalarray; console.log($ scope.globalarray)但仍未定義。並且當我檢查了console.log(globalarray.length)的長度時,它將返回0,但是console.log(globalarray)返回給我2值 –

1

通過上面顯示的示例。

  1. 您不推送globalarray中的值。通過不呼叫abc()功能。

  2. 如果您的globalarray變量是窗口級別的全局變量,它將在您的角度應用程序中的任何地方可用。

請觀察我演示過的小提琴的行爲。我也做了一個plunk所以你會清楚地理解。

Fiddle

var globVar = [12 ,33]; 
var myApp = angular.module('myApp', []);  
myApp.controller('MyController', function($scope) {  
    $scope.globVar = globVar; 
}); 

更新

由於數據進來,你需要重新運行摘要週期和更新變量的回調。請注意這個小提琴,因爲它有你的座標init。

// we got the cords 
var cords = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326'); 

// we get the scope from the element that has the controller binded to it. 
var scope = angular.element(document.getElementById("MainWrap")).scope(); 

// we call a digest cycle of angular to update our scope variable sinse it comes in a callback when angular is loaded 
scope.$apply(function() { 
    scope.updateCords(cords); 
}); 

Working Fiddle

現在希望它幫助。

+0

@ v.josh請參閱更新後的答案和附帶的小提琴。我試圖儘可能描述性。 –

+0

可以請你檢查一下我已更新的plunker然後你可以得到我想要的確切想法 –

+0

@ M.JunaidSalaat不推薦使用'scope()'方法,並且[在生產中運行時]不會工作(https:/ /docs.angularjs.org/guide/production)用'$ compileProvider.debugInfoEnabled(false);'。 –

1

試試這個:

$scope.globalarray = []; 

function abc() { 
    var array = [4,5] 
    angular.forEach(array,function(k,v){ 
    $scope.globalarray.push(k); 
    }) 

} 
1

看看這個鏈接:

AngularJS access scope from outside js function

,你可以用不同的方式簡單地想起來了,一旦你進入範圍,你使用數組中的數據在控制器內調用setter方法,然後可以執行所需的任何操作。我在項目中使用了這種方法,效果很好。

例如:

// externalScope是在角CONTROLER聲明的全局變量 //這是從的角度 外部訪問控制器和觸發方式的方式//我們想要的是通知有些數據已被改變,所以它可以做一些事情。在我的例子中,selectedUserValue是我傳遞給控制器​​的數據位。

if (externalScope) { 
    externalScope.$apply(function() { 
     externalScope.selectUser(selectedUserValue); 
    }); 
} 

這個代碼存在wherver你試圖調用範圍的方法有一些數據在控制器外部

現在的實際控制人,我們有這樣的事情:

var externalScope; 

    (function() { 
    controller code 

某處控制器:

externalScope = $scope; //this exposes the scope to the outside world 

請注意externalScope變量的聲明方式控制器以外的紅色,所以它將是全球性的。

之後,只需在外部調用的角度法中使用任何需要的代碼即可。

在我的情況

這是一個二傳手,然後調用別的東西,這使得使用該資料:

$scope.selectUser = function (userID) { 
      if (userID && userID !== $scope.selectedUserID) { 
       $scope.selectedUserID = userID; 
       $scope.loadUserRecords(); 
      } 
     }; 

希望這是有道理的。免責聲明......這不是我稱之爲微不足道的事情,只有在你沒有其他選擇的情況下才能使用!

+0

我已經嘗試過使用$ apply functron,但它仍然不能正常工作,請你檢查我已經更新的plunker。 –

相關問題