# $htttp.get doesn't return proper data when it's in services.js

**URL:** <https://forum.ionicframework.com/t/htttp-get-doesnt-return-proper-data-when-its-in-services-js/20360>\
**Category:** ionic-v1\
**Created:** [April 6, 2015, 3:47pm UTC](https://forum.ionicframework.com/t/htttp-get-doesnt-return-proper-data-when-its-in-services-js/20360 "2015-04-06T15:47:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pacjonek](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Pacjonek](https://forum.ionicframework.com/u/Pacjonek)\
**Post date:** [April 6, 2015, 3:47pm UTC](https://forum.ionicframework.com/t/htttp-get-doesnt-return-proper-data-when-its-in-services-js/20360/1 "2015-04-06T15:47:12Z")

</div>

Hi friends,

I’ve got **tab-news.html** :

```
<ion-view view-title="Wiadomości">
  <ion-content class="padding">
    <div class="list card" ng-repeat="article in news">
      <div class="item item-divider"><b>{{article.header}}</b></div>
      <div class="item item-body">{{article.text}}</div>
    </div>
  </ion-content>
</ion-view>

```

and **controlers.js** :

```
.controller('NewsCtrl', function($scope, $http) {
	$http.get('http://www.patryk.t15.org/zsti/news.php').success(function(response){
		$scope.news=response;
	})
})

```

All works fine, I get list with data in my app, items are showing

But when $http.get function is in **services.js** like here:

```
angular.module('starter.services', [])
.factory('News', function($http) {
	var news=[];
	
	$http.get('http://www.patryk.t15.org/zsti/news.php').success(function(response){
		news=response
	})
	
	return {
    all: function() {
      return news;
    }
  };
})

```

and controlers.js looks like:

```
angular.module('starter.controllers', [])
.controller('NewsCtrl', function($scope, News) {
	$scope.news=News.all()
})

```

It doesn’t work ☹ List in my app is empty. There is no error in Firebug.  
Probably I made some a stupid mistake but I don’t know where.

Thanks in advance for help 🙂

---

<div class="post-metadata">

**Author:** ![joseadrian](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/joseadrian/32/71106_2.png) [@joseadrian](https://forum.ionicframework.com/u/joseadrian)\
**Post date:** [April 6, 2015, 3:57pm UTC](https://forum.ionicframework.com/t/htttp-get-doesnt-return-proper-data-when-its-in-services-js/20360/2 "2015-04-06T15:57:31Z")

</div>

That’s because the [http](https://docs.angularjs.org/api/ng/service/%24http) call is async. Read about [$q](https://docs.angularjs.org/api/ng/service/%24q) which is a service that helps you in these scenarios (promises).

Change your factory as follows:

```auto
.factory('News', function($http) { 
  return {
    all: function() {
      return $http.get('http://www.patryk.t15.org/zsti/news.php');
    }
  };
})

```

And your controller:

```auto
$scope.news = [];

News.all().then(function(news) {
  $scope.news = news;
});

```

---

<div class="post-metadata">

**Author:** ![Pacjonek](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Pacjonek](https://forum.ionicframework.com/u/Pacjonek)\
**Post date:** [April 13, 2015, 5:46pm UTC](https://forum.ionicframework.com/t/htttp-get-doesnt-return-proper-data-when-its-in-services-js/20360/3 "2015-04-13T17:46:15Z")

</div>

Thank you very much. From now I know what are promises and where i made mistakes 😄
