[英]AngularJs watchers why is this happening
ok I am struggling with this for a while now 好吧,我现在为此苦了一段时间
http://plnkr.co/edit/AjNTek2EiiAwQUsfP5XQ?p=preview http://plnkr.co/edit/AjNTek2EiiAwQUsfP5XQ?p=preview
app.controller('MainCtrl', function($scope, $q, $window) {
function getPosition() {
var deferred = $q.defer();
if ($window.navigator.geolocation) {
$window.navigator.geolocation.watchPosition(function(position) {
deferred.resolve(position);
});
}
return deferred.promise;
}
$scope.homePosition = {
coords: {
latitude: 22,
longitude: 25
}
};
var promise = getPosition();
$scope.showDistance = function(pos, homePosition) {
var position = {
lat: pos.coords.latitude,
lon: pos.coords.longitude
};
var homePosition = {
lat: homePosition.coords.latitude,
lon: homePosition.coords.longitude
};
/** Converts numeric degrees to radians */
function getDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = (lat2 - lat1).toRad(); // Javascript functions in radians
var dLon = (lon2 - lon1).toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
};
return getDistance(position.lat, position.lon, homePosition.lat, homePosition.lon);
};
$scope.s = function() {
$scope.$watch(function() {
return $scope.homePosition.coords.latitude;
}, function(newValue, oldValue) {
if (newValue) {
$scope.distance = $scope.showDistance($scope.position, $scope.homePosition);
}
})
}
promise.then(function(position) {
$scope.position = position;
$scope.distance = $scope.showDistance(position, $scope.homePosition);
$scope.s();
});
});
when the watcher is not fired everything works ok when the watcher is fired I get a lat2.toRad() is not a function (took a while to find that.. in chrome I only got undefined is not a function.. thank you Firebug for being precise) which lead me to suspect something is happening with the 当观察者没有被解雇时,一切正常,当观察者被解雇时,我得到了lat2.toRad()不是一个函数(花了一段时间才能找到它。.在chrome中,我只有undefined不是函数。.谢谢Firebug精确)导致我怀疑
return this*Math.pi/180
is what I am saying valid? 我说的有效吗? and if someone would point out what I am missing here in terms of Angular or Javascript thank you 如果有人指出我在Angular或Javascript方面缺少的内容,谢谢
I think you need to rethink your logic a little. 我认为您需要重新考虑一下自己的逻辑。 You have a number of issues in the code that even Plunkr is picking up on, like redefining homePosition in your showDistance method: 甚至Plunkr都在代码中遇到许多问题,例如在showDistance方法中重新定义homePosition:
...
$scope.showDistance = function(pos, homePosition) {
...
// Here we define a new variable with the same name as our function's 2nd param
var homePosition = {
...
This isn't the root cause of your problem - I'm just calling it out because these warnings are often indications of practices that will get you into trouble in Javascript. 这不是问题的根本原因-我只是在说出来,因为这些警告通常表示会给您带来JavaScript麻烦的实践指示。 Javascript has something called "variable hoisting" that basically means no matter where in your function you define a var, it will actually get defined at the very top, when you enter the function. Javascript有一种称为“变量提升”的方法,它的基本含义是,无论您在函数中的哪个位置定义var,当您输入该函数时,它实际上都会在顶部定义。 This can create very unpredictable behavior if you haven't allowed for it, and re-defining a function parameter with a var of the same name is one of those gotchas that trip a lot of people up. 如果您不允许这样做,则可能会导致无法预测的行为,并且使用相同名称的var重新定义函数参数是使很多人绊倒的陷阱之一。
Your root cause could not be simpler, though. 不过,您的根本原因再简单不过了。 In this case the debugger is your friend. 在这种情况下,调试器是您的朋友。 I've forked your Plnkr and slightly modified it to add a debugger breakpoint: http://plnkr.co/edit/6amCdnBYqmzlUXVO9buP?p=preview 我已经分叉了您的Plnkr,并对其进行了少许修改以添加调试器断点: http ://plnkr.co/edit/6amCdnBYqmzlUXVO9buP?p=preview
If you look at your "locals" panel when you hit this point you will see that lat2 is a string. 如果您在到达这一点时查看“本地”面板,您会看到lat2是一个字符串。 Strings objects don't support math functions - you need to convert it to an int or float first. 字符串对象不支持数学函数-您需要先将其转换为int或float。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.