2015-10-08 1 views
-1

Я получаю де геолокации пользователя, таким образом, и то работает у может увидеть его в представлении с {{ш}} и {{}} LNG:Как передать параметры в angularjs?

(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-адресами на основе результатов локации. У меня есть этот код, но doen't работы, потому что он говорит, что широта и LNG является undefinied Может быть, я просто звоню широты и долго в неправильном

Я звоню его в представлении, как

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

ли код, определяющий функцию $ scope.showTrends под

тега в вашем HTML? – buzzsaw

+0

no no, у меня есть все js-код, показанный в моем контроллере, и тег раздела предназначен только для того, чтобы показать способ, которым я его называю в моем представлении. –

+0

«Не работает»: означает ли это, что $ scope.lat является undefined? Является ли ваша функция showTrends в том же контроллере? – Bigood

ответ

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

}());