是否有方法從角度內的foreach函數內部訪問範圍?可從內部foreach函數訪問的角度範圍
$scope.getPlowHistory = function() {
$scope.plowId = $stateParams.plowId;
SnowPlowService.getPlowHistory($scope.plowId).then(function(response) {
$scope.plow = response;
angular.forEach($scope.plow,function(value,index){
$scope.Lat = value.Latitude;
$scope.Lng = value.Longitude;
});
});
};
我需要$ scope.Lat和$ scope.Long是在$ scope.getPlowHistory()可用,而不僅僅是foreach循環中。
謝謝!
$ scope _should be_access,基於javascript關閉,除非在promise解決之前範圍被破壞。你遇到了什麼錯誤? –
嘗試使用$ scope.getPlowHistory = function(){... console.log($ scope.Lat);};在循環之外。 – nrunit
訪問它沒有問題,但是你覆蓋循環的每個迭代中的變量。 – charlietfl