![](/img/trans.png)
[英]how to access a variable from outside the $scope function angular javascript?
[英]how to update scope variable of a controller from a outside JavaScript function
我是angularJS的新手。 我對如何從外部JavaScript函數更新作用域變量進行了說明。 我找不到任何方法可以幫助我,以下是我的代碼段供參考
var devices = {};
mainApp.controller('deviceDataController', function($scope) {
console.log(devices);
$scope.deviceDetails = devices;
});
function deviceFunc (data,index){ //here the data and index values are obtain after doing a for each for an array and data is an object
device['device'+index] = data;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
現在,如果我在console.log行上運行代碼,我會得到“ object {}”,但是里面有值,但是當我將對象分解為它的第一個屬性時,它表示未定義。 console.log(devices [device0] .devicename)給我未定義。
如果有什么可以做的,請幫我。
您不應該這樣做,因為這不是管理數據的有角度的方法。 嘗試重新構建代碼。
但是您仍然可以通過許多不同的方式來做到這一點。
示例:如果要從外部訪問范圍,請嘗試:
angular.element(someElement).scope();
完整文檔: https : //docs.angularjs.org/api/ng/function/angular.element
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.