title: Angularjs, getting data from factory
body: <p>I have a factory where i am trying to get data by $http service. </p>
  
  <p>factory:</p>
  
  <pre><code>(function(){
  angular
  .module('projectApp')
  .factory('weatherfactory', weatherfactory);
  
  weatherfactory.$inject=['$http'];
  
  function weatherfactory($http){
  var cities=[
    {name: "Łódź", link: "http://api.openweathermap.org/data/2.5/weather?q=Lodz,pl"},
    {name: "Warszawa", link: "http://api.openweathermap.org/data/2.5/weather?q=Warszawa,pl"}
  
  ];
  var weat={};
  
  var service={
    getCities: getCities,
    GetWeather: _GetWeather
  };
  return service;
  
  function _GetWeather(link){
    $http.get(link).success(function(data){
      weat=data;
    });
    return weat;
  }
  
  
  function getCities(){
    return cities;
  }
  </code></pre>
  
  <p>}
  })();</p>
  
  <p>In controller i call function from factory:</p>
  
  <pre><code>function weatherData(city){
    sa.weather=weatherfactory.GetWeather(city);
    sa.show=true;
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;div class="col-md-8"&gt;
        &lt;select class="form-control" ng-model="sa.city"&gt;
          &lt;option ng-repeat="city in sa.cities" value="{{city.link}}"&gt;{{city.name}}&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
      &lt;div class="col-md-4"&gt;
        &lt;button class="btn btn-primary" ng-click="sa.weatherData(sa.city)"&gt;Wybierz&lt;/button&gt;
      &lt;/div&gt;
      &lt;div ng-if="sa.show"&gt;
        {{sa.weather.name}}
      &lt;/div&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>It works but not correctly. I have to click button twice to show correct data. </p>