我怎样才能传递angularjs中的参数?

时间:2015-10-08 16:17:57

标签: angularjs function view parameters controller

我正在通过这种方式获得用户的地理位置,并且工作人员可以使用{{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);
                    })
            }

}
})

现在,我需要根据位置结果在不同的网址之间进行选择。我有这个代码但是没有用,因为它说lat和lng是不确定的 也许我只是在调用纬度并且长期在错误的

我在视图中调用它

<section ng-init="showTrends()"></section>

1 个答案:

答案 0 :(得分:0)

最后我做到了!这是我在视图控制器中的新代码

(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);

}());