1

I'm trying to fetch data (units) for a route (app.units). Here's the code.

  .state('app.units', {
    url: '/units',
    views: {
      'menuContent' :{
        templateUrl: "templates/units.html",
        controller: 'UnitCtrl',
        resolve:{
          units: function($stateParams, ExamService, $rootScope, $http){
            return $http.get(ApiEndpoint.url +'/units.json?auth_token='+$rootScope.globals.currentUser.userToken)
              .success(function(response){
                //console.log('hello');
                return response.data;
              })
          }
        }
      }
    }
  })

Fails to resolve without errors End point works in Advanced Rest Client

As you can see this won't resolve, doesn't show any errors in the console and gives me a blank page in my ionic app, yet the endpoint works with Advanced Rest Client for chrome. I've been stuck on this for age could someone please figure this out.

Ingadi
  • 563
  • 1
  • 7
  • 20
  • Perhaps the `$http` call is failing. Which in the case of ui-router and resolves will show nothing. See my other answer here: http://stackoverflow.com/questions/21868219/state-transition-after-rejected-promise-angular-ui-router/21869209#21869209 – Matt Way Nov 27 '15 at 11:17
  • can you show the function param and dependencies of UnitCtrl ? – krish Nov 27 '15 at 11:24
  • @MattWay is right this may be the case. – krish Nov 27 '15 at 11:48

1 Answers1

0

Hey i just refactored your code. Try it

.state('app.units', {
    url: '/units',
    views: {
      'menuContent' :{
        templateUrl: "templates/units.html",
        controller: 'UnitCtrl',
        resolve:{
          units : ['$stateParams','$q','ExamService','$rootScope', '$http',function ($stateParams,$q,ExamService,$rootScope, $http) {
            var deferred = $q.defer();
            $http.get(ApiEndpoint.url +'/units.json?auth_token='+$rootScope.globals.currentUser.userToken)
              .success(function(response){
                //console.log('hello');
                if(response!=undefined)
                  deferred.resolve(response);
                else
                  deffered.reject();
                })
             return deferred.promise;
           }]
        }
      }
    }
  })
krish
  • 537
  • 2
  • 14
  • Adding the deferred is unnecessary. – Matt Way Nov 27 '15 at 11:44
  • This still doesn't work. It shows the same blank result. What's weird is without using resolves, that is, implemented in a controller it works and fetches the required data. I don't get it. – Ingadi Nov 29 '15 at 09:05