當我插入console.log($scope)
到我的代碼,我得到以下結果:如何訪問角度範圍內的變量?
$get.k.$new.a.$$childScopeClass.$$childScopeClass {$$childTail: null, $$childHead: null, $$nextSibling: null, $$watchers: Array[4], $$listeners: Object…}
$$childHead: null
$$childScopeClass: null
$$childTail: null
$$listenerCount: Object
$$listeners: Object
$$nextSibling: null
$$prevSibling: $get.k.$new.a.$$childScopeClass.$$childScopeClass
$$watchers: Array[4]
$id: "005"
$parent: Object
Bad: false
Good: true
Search: function() {
address: "63146"
focus: "63146"
this: $get.k.$new.a.$$childScopeClass.$$childScopeClass
__proto__: Object
我感興趣的變量是Good: true
。但是,當我在下一行呼叫console.log($scope.Good)
時,它將返回false
。
如何調用上述在控制檯中返回true的「Good」變量?
編輯: 控制器
app.controller('locationController', function ($scope) {
$scope.Good = false;
$scope.Bad = false;
var mapOptions = {
center: { lat: 38.68, lng: -90.46 },
zoom: 8
};
var image = {
url: 'app/assets/img/marker.png'
}
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
$scope.Search = function() {
$scope.Good = false;
$scope.Bad = false;
var address = $scope.address;
var radius = parseInt(50, 10) * 1000;
var marker_start = new google.maps.Marker({
position: { lat: 38.688757, lng: -90.464391 },
map: map,
icon: image,
title: ""
});
var fill = '#fff';
var populationOptions = {
strokeColor: '#66FF99',
strokeOpacity: 0.2,
strokeWeight: 2,
fillColor: fill,
fillOpacity: 0.35,
map: map,
center: new google.maps.LatLng(38.68, -90.46),
radius: 80000
};
var lat = '';
var lng = '';
var geocoder = new google.maps.Geocoder();
var marker_user = null;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
marker_user = new google.maps.Marker({
position: { lat: lat, lng: lng },
map: map,
animation: google.maps.Animation.DROP,
title: "Your Location"
});
if (google.maps.geometry.spherical.computeDistanceBetween(marker_user.getPosition(), marker_start.getPosition()) < 80000)
$scope.$apply(function() { $scope.Good = true; });
else
$scope.$apply(function() { $scope.Bad = true; });
}
});
console.log($scope);
console.log($scope.Good);
console.log($scope.Bad);
var cityCircle = new google.maps.Circle(populationOptions);
};
});
向我們展示你的控制器 – hurricane
編輯,包括控制器。 – cul8r
當您將一個「對象」記錄到控制檯時,這是一個常見問題。控制檯向您顯示該對象的瞬間值。我會試着找到一個現有答案,解釋爲什麼你看到你看到的東西,如果有的話,請回到這裏。 –