本文介绍了$范围嵌套函数无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图建立一个服务于angularjs地理位置,但是我想试一试我的控制器首先然后移动它。现在我不能当我拿到我的position.coords.latitude /经度值来访问$范围的变量。

I'm trying to build a service for geolocation in angularjs, however I wanted to give it a try in my controller first then move it out. Right now I'm not able to access the $scope variable when I get my position.coords.latitude/longitude values.

能向我解释人为什么它无法访问?我没有在JavaScript中深入了解,但它是因为父变量不是出于某种原因访问?并会是怎样来解决这种限制的好办法?

Can someone explain to me why it's not accessible? I don't have in depth knowledge in javascript, but is it because parent variables aren't accessible for some reason? and What would be a good way to work around this kind of limitation?

我标记这个JavaScript太,因为我不知道,如果这是一个语言的设计特征什么的,我只是缺少,因为我瞎了。

I'm tagging this javascript too since I'm not sure if this is a language design characteristic or something I'm just missing because I'm blind.

我看到纬度/长时间打印到控制台上,所以我知道我得到有效的数据。

I do see the lat/long print to the console so I know I'm getting valid data.

//geolocation.js

angular.module('myapp', [])
    .controller('MyController', ['$scope',
        function($scope) {
            if ("geolocation" in navigator) {
                var watchID = navigator.geolocation.watchPosition(function(position) {
                    $scope.latitude = position.coords.latitude;
                    $scope.longitude = position.coords.longitude;
                    console.log($scope.latitude);
                    console.log($scope.longitude);
                }, function() {}, {
                    enableHighAccuracy: true,
                    maximumAge: 30000,
                    timeout: 27000
                });
            } else {
                /* geolocation IS NOT available */
            }
        }
    ]);

//index.html
<!doctype html>
<html ng-app="myapp">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js">
    </script>
    <script src="geolocation.js">
    </script>
</head>

<body ng-controller="MyController">
    Hello
    <div>
        <p>{{latitude}}</p>
        <p>{{longitude}}</p>
    </div>
</body>

</html>

好了,所以我增加了

Ok so I added

$ $范围申请($ scope.latitude);

$scope.$apply($scope.latitude);

右后的console.log的,但仍然在函数。

Right after the console.log's but still in the function.

两者的纬度和经度的更新。我想只是一个香草$ $范围适用于()。但没有做任何事情。我想这是工作的第一种方式,所以我会与坚持。

Both latitude and longitude update. I tried just a vanilla $scope.$apply(); but that didn't do anything. I guess it's working the first way so I'll stick with that.

我已经更新至$ scope.coords = position.coords现在$ $范围申请($ scope.coords);

I have updated to $scope.coords = position.coords and now $scope.$apply($scope.coords);

在HTML我做{{coords.latitude}}和经度等,并能正常工作。

in the html I'm doing {{coords.latitude}} and longitude etc, and it works fine.

有关有兴趣的人。我也正打算搬到不知道如何将发挥出来的服务。

For anyone interested. Also I'm planning to move to a service not sure how that'll play out.

推荐答案

那么,你所看到的值打印到控制台,所以你知道你其实访问 $范围变量。你只是不碰巧看到的变化在HTML,是否正确?因为角度是不知道的范围正在改变你没有看到这些变化。简短的回答是,你需要用变化的范围与 $适用让角知道他们正在被改变。

Well, you're seeing the values print out to the console so you know that you are in fact accessing the $scope variable. You just don't happen to see the changes in the HTML, correct? You're not seeing these changes because angular isn't aware that the scope is being changed. The short answer is, you need to wrap changes to the scope with $apply to let angular know they're being changed.

下面是一个相关的回答对您有所帮助:

Here's a related answer you may find useful: Using scope.$watch and scope.$apply

这篇关于$范围嵌套函数无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:58