【发布时间】:2015-10-08 16:17:57
【问题描述】:
我正在以这种方式获取用户的地理定位,并且可以使用 {{lat}} 和 {{lng}} 在视图中看到它:
(function () {
var showController = function($scope, $http, $routeParams) {
$scope.showPosition = function (position) {
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
$scope.$apply();
}
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
$scope.error = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
$scope.error = "Location information is unavailable."
break;
case error.TIMEOUT:
$scope.error = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
$scope.error = "An unknown error occurred."
break;
}
console.log ("error geo "+$scope.error);
$scope.$apply();
}
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
}
else {
$scope.error = "Geolocation is not supported by this browser.";
}
}
$scope.getLocation();
/** Here says lat and lng undefinied*/
$scope.showTrends = function() {
var url;
if ($scope.error) {
url = "http://localhost:3000/trends?id=23424747"
}
else {
url = "http://localhost:3000/myplace?lat="+$scope.lat+"&long="+$scope.lng;
console.log(url);
}
$http.get(url)
.then(function(res){
$scope.trends = res.data[0].trends;
console.log(res.data[0].trends);
})
}
}
})
现在,我需要根据位置结果在不同的 url 之间进行选择。我有这段代码但不起作用,因为它说 lat 和 lng 未定义 也许我只是把纬度和经度叫错了
我在视图中这样称呼它
<section ng-init="showTrends()"></section>
【问题讨论】:
-
代码是否在您的 HTML 中的
标签下定义了 $scope.showTrends 函数? -
不不,我的控制器中显示了所有 js 代码,section 标签只是为了显示我在视图中调用它的方式
-
“不起作用”:这是否意味着 $scope.lat 未定义?您的
showTrends函数是否在同一个控制器中? -
是的,它表示纬度和经度未定义。并且所有功能都在同一个控制器中
标签: angularjs function view parameters controller