Hello,
I am using $http.get to load a list on the main view of my application. Now that I want to display a view with item details (when you tap on one element of the list), I see that I should implement a service to share data between views.
I have looked at: http://learn.ionicframework.com/formulas/data-the-right-way/#
However it is using a static list of elements. I can implement this, but I dont see how to use this in combination with a http request to fetch JSON data.
In other words, Can you help me combine these 2 pieces of code?
.controller('AdsCtrl', function($scope, $http, ads) {
$scope.name = 'World';
$scope.allads = [];
$scope.ads = [];
$http.get('http://www.mysite.com/list.json').success(function(data) {
$scope.allads = data;
$scope.loadMore();
});
var counter = 0;
$scope.loadMore = function() {
var data = [];
var l = $scope.ads.length;
var m = Math.min(l+10, $scope.allads.length);
for ( var i = l; i < m; i++) {
data.push($scope.allads[i]);
}
console.log('Loading more!', m);
$scope.ads = $scope.ads.concat(data);
$scope.$broadcast('scroll.infiniteScrollComplete');
};
$scope.$on('stateChangeSuccess', function() {
$scope.loadMore();
});
// THIS METHOD LOADS 4 LOCAL TEST ITEMS WITH THE ADSSERVICE
//$scope.ads = ads;
})
and
.service('AdsService', function($q) {
return {
ads : [
{ shortrelativetime:'30 min ago', title: 'TST อาหารเสริมเพี่มความสูง', group:'Vehicles', category:'Cars', province:'Vientiane Capital', district:'Xaythany', price:'24000', currency:'USD', photo:'20140202-040753-c1395444fd.jpg', id: 1 },
{ shortrelativetime:'1 day ago', title: 'TST Samsung Galaxy Grand', group:'High Tech', category:'Phones', province:'Vientiane Capital', district:'Xaythany', price:'6000', currency:'THB', photo:'20140218-101800-1602504d17.jpg', id: 5 },
{ shortrelativetime:'1 day ago', title: 'TST ຂາຍດິນປຸກສ້າງ', group:'Real Estate', category:'Land or House', province:'Vientiane Capital', district:'Xaythany', price:'850000', currency:'THB', photo:'20140715-080420-4a0ba40b89.jpg', id: 6 },
{ shortrelativetime:'08 Jun 2014', title: 'TST HTC One', group:'High Tech', category:'Phones', province:'Vientiane Capital', district:'Xaythany', price:'12000', currency:'THB', photo:'20140604-083644-046276fe30.jpg', id: 9 }
],
getAds: function() {
return this.ads
},
getAd: function(adId) {
var dfd = $q.defer()
this.ads.forEach(function(ad) {
if (ad.id === adId) dfd.resolve(ad)
})
return dfd.promise
}
}
})
Thanks a lot for the tips!

