为什么AngularJS服务没有绑定到范围?

时间:2014-08-09 03:03:50

标签: javascript angularjs binding geolocation

Angular noob尝试使用Angular 1.2.21按照this geolocation示例逐步查看watchPosition。浏览器调试器显示服务位置对象在GPS触发时更新,但范围myCoords仍未定义。我尝试在几个地方添加$ apply,其中投掷正在进行中。这里有什么不对,我需要阅读哪些概念?

angular
    .module('myApp', ['ngGeolocation'])
    .controller('geolocCtrl', ['$geolocation', '$scope', function($geolocation, $scope) {
        $geolocation.watchPosition({
            timeout: 60000,
            maximumAge: 250,
            enableHighAccuracy: true
        });
        $scope.myCoords = $geolocation.position.coords; // not updated
        $scope.myError = $geolocation.position.error; // this becomes truthy, and has 'code' and 'message' if an error occurs
    }]);

1 个答案:

答案 0 :(得分:2)

每次更新都会重新分配coords的{​​{1}}属性。它是$geolocation.position $ geolocation`保持不变。

所以,(基于你想如何使用它)你应该写这样的东西:

position object of