显示$ scope并不适用于我的视图

时间:2016-06-04 18:33:23

标签: angularjs cordova ionic-framework

我试图在我的视图中显示$ scope值,但它确实有效。

我的console.log与范围有效。

有什么想法吗?

在我的Geolocation Plugin控制器

    .controller('geoCtrl', ['$scope', '$ionicPlatform', function($scope, $ionicPlatform) {
    function updateLoc($scope)
    {
        $scope.latitude2 = "wow";
        console.log("hello");
        console.log($scope.latitude);
        console.log($scope.longitude);
        console.log($scope.speed);
    }
        $scope.findGeolocation = function() {
            console.log("navigator.geolocation works");
            var onSuccess = function(position) {
                $scope.latitude = position.coords.latitude;
                $scope.longitude = position.coords.longitude;
                $scope.altitude = position.coords.altitude;
                $scope.speed = position.coords.speed;
                $scope.timestamp = position.timestamp;
                console.log($scope.longitude);
            updateLoc($scope);

        };
        function onError(error) {
            alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');

        };
        navigator.geolocation.getCurrentPosition(onSuccess, onError);   
    }
    $ionicPlatform.ready(function() { $scope.findGeolocation(); })

}])

在我看来

<ion-view view-title="APP">
  <ion-content>
  <div ng-controller="geoCtrl">
    <p>Bonjour, {{ latitude2 }} </p>
    <p id="speed">Finding speed...</p>
    <p id="latitude">Finding latitude...</p>
    <p id="longitude">Finding longitude...</p>
   </div>
  </ion-content>
</ion-view>

谢谢

1 个答案:

答案 0 :(得分:1)

地理定位回调函数未在摘要中运行 - 您需要告知角度已进行更新。即。

        var onSuccess = function(position) {
            $scope.$apply(function() {
                $scope.latitude = position.coords.latitude;
                $scope.longitude = position.coords.longitude;
                $scope.altitude = position.coords.altitude;
                $scope.speed = position.coords.speed;
                $scope.timestamp = position.timestamp;
                updateLoc($scope);
            });
        };