Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs json URL changer

I'm working on some mega simple weather app in Angular for practice reasons and i'm stuck..

i have a angular json feed like this:

app.factory('forecast', ['$http', function($http) { 
return $http.get('http://api.openweathermap.org/data/2.5/weather?q=Amsterdam,NL&lang=NL_nl&units=metric') 
        .success(function(data) { 
          return data; 
        }) 
        .error(function(err) { 
          return err; 
        }); 
}]);

and it loads the feed in to the index.html. its all working and what i wand now is a input form on index that changes the Amsterdam part of the url on js/services/forcast.js where the above code is to another city so people can see their city weather.

See the demo here: http://dev.bigstoef.nl/workspace/shiva/weer/index.html

Ive tryd about all posable options about now and i'm 3 days further and its a no go. What is there correct way here?

like image 380
Shiva Traanman Avatar asked May 26 '15 07:05

Shiva Traanman


1 Answers

First, create a "configurable" service :

app.factory('forecast', ['$http', function($http) { 
    var city;
    var cities = {
        amsterdam: 'Amsterdam,NL',
        paris: 'Paris,FR'
    };
    var api_base_url = 'http://api.openweathermap.org/data/2.5/weather';
    var other_params = 'lang=NL_nl&units=metric';

    return {
        setCity: function(cityName){
            city = cityName ;
            console.log(city);
        },
        getWeather: function(cityName){
          console.log(city);
            if(cityName) this.setCity(cityName);
            if (!city) throw new Error('City is not defined');
            return $http.get(getURI());
        }
    }

    function getURI(){
        return api_base_url + '?' + cities[city] + '&' + other_params;
    }
}]);

Then you can create a controller like the following:

app.controller('forecastCtrl', ['$scope', 'forecast',function($scope,forecast){

$scope.city = 'amsterdam' ;

    $scope.$watch('city',function(){
      console.log($scope.city);
      forecast.setCity($scope.city);
    });

$scope.getWeather = function(){
  console.log('get weather');
    forecast.getWeather()
    .success(function(data){
    console.log('success',data);
    $scope.weatherData = data;
    }).error(function(err){
      console.log('error',err);
      $scope.weatherError = err; 
    });    
}; 
}]);

To implement a template as the following

<link rel="stylesheet" href="style.css" />

<div data-ng-controller="forecastCtrl">

  <form>

    <label>
      <input type="radio" name="city" data-ng-model="city" data-ng-value="'amsterdam'">Amsterdam
    </label>
    <br/>
    <label>
      <input type="radio" name="city" data-ng-model="city" data-ng-value="'paris'">Paris
    </label>
    <br/>

    <button data-ng-click="getWeather()">Get Weather</button>

  </form>  

  <p class="weather-data">
    {{weatherData}}
  </p>
  <p class="weather-error">
    {{weatherError}}
  </p>

</div>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="script.js"></script>

You can view the code working here : http://plnkr.co/edit/rN14M8GGX62J8JDUIOl8?p=preview

like image 150
Rémi Becheras Avatar answered Sep 22 '22 21:09

Rémi Becheras