我如何在angularjs中传递参数?
Posted
技术标签:
【中文标题】我如何在angularjs中传递参数?【英文标题】:How can i pass the parameters in angularjs? 【发布时间】: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 中的我终于做到了!这是我在视图控制器中的新代码
(function ()
var trendsController = function($scope, $http, $routeParams, $q)
$scope.trends = [];
var latitude = 0, longitude = 0, placeId=0,trd = this;
this.getLocation = function()
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(trd.getTrends);
else
console.log("Geolocation is not supported by this browser.");
this.getTrends = function(position)
getPlaceId(position)
.then(function(id)
placeId = id;
getTrendList(placeId)
.then(function(trends)
$scope.trends = trends;
)
.catch(function(err)
console.log(err);
);
)
.catch(function(err)
console.log(err);
);
;
getTrendList = function(placeId)
var def = $q.defer(),
trendList = [];
$http.get("http://localhost:3000/trends?id="+placeId)
.success(function(response)
trendList = response[0].trends;
def.resolve(trendList);
).error(function()
def.reject("Failed to get trend list");
);
return def.promise;
getPlaceId = function (position)
var def = $q.defer();
var latitude = position.coords.latitude,
longitude = position.coords.longitude;
$http.get("http://localhost:3000/myplace?lat="+latitude+"&long="+longitude).success(function(response)
placeId = response[0].woeid;
def.resolve(placeId);
).error(function()
def.reject("Failed to get id location");
);
return def.promise;
trd.getLocation();
trendsController.$inject = ["$scope", "$http", "$routeParams","$q"];
angular.module("tweeterApp").controller("trendsController", trendsController);
());
【讨论】:
以上是关于我如何在angularjs中传递参数?的主要内容,如果未能解决你的问题,请参考以下文章