如何在角度范围内访问变量?

时间:2015-05-09 22:07:42

标签: angularjs scope

当我在代码中插入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

如何拨打上述" Good"在控制台中返回true的变量?

编辑: 的控制器

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);
    };
});

1 个答案:

答案 0 :(得分:3)

Good仅在传递给geocoder.geocode()的回调函数后才变为。但是,在要求地理编码器进行地理编码后,调用之前打印它

  1. 要求地理编码器进行地理编码。这会向Google发送HTTP请求
  2. 打印好:它还是假的
  3. 当Google的回复进入时,回执执行且Good变为真