How to use ui-router's .resolve within ionic tabs

I have been trying to resolve a $http request within the $stateProvider.state declaration which works well. console.log returns the JSON data, but I cannot seem to access it from within the controller.

I created a tabs app using ionic cli

Here’s what I got.

$stateProvider
.state('tab.search', {
  url: '/search',
  resolve: {
  	estates: ["$http", function($http){
  	    $http.get('/data/Estates.json').then(function(res) {    
                console.log(res) // shows the array        
                return res.data;
            });
  	}]
  },   
  views: {
    'tab-search': {
      templateUrl: 'templates/tab-search.html',
      controller: 'FriendsCtrl'
    }
  }
})

and the controller is simply

.controller('FriendsCtrl', ["$scope", "estates", function($scope, estates) {
	alert(JSON.stringify(estates)) // undefined
}]);

What am I doing wrong? The alert(estates) returns undefined!

EDIT

I tried putting the resolve within the views object and it still returns undefined

Take a look… View and navigation states and resolve/promise

May this help you.

  estates: ["$http", function($http){
         return $http.get('/data/Estates.json').then(function(res) {    
            console.log(res) // shows the array        
            return res.data;
        });
  }]

My only change was to add a return statement and it worked fine for me

2 Likes

I can’t believe I missed that. :slight_smile:

it is subtle difference, it took me a while to realize that was the problem

1 Like

How would I do this is the service /data/Estates.json took some query params?

can u be more specific?

I just meant adding regular ?key1=value1&key2=value2 params. I’m good not though, Thanks for following up.