2

I am trying to load a get service JSON function in the main state resolve function so I can store the data to a scope variable.

The account JSON information is relevant because all sub pages are essentially dependent on the information.

--

The below code is partially working. The account resolve function is being successfully called and even the $http returns a promise (state === 0 though). The issue is when the account function resolves the state.controller is never being called.

$stateProvider
            .state('app',{
                url: '/',
                views: {
                    'header': {
                        templateUrl: '../views/templates/partials/header.html',
                    },
                    'content': {
                        templateUrl: '../views/templates/partials/content.html' 
                    },
                    'footer': {
                        templateUrl: '../views/templates/partials/footer.html',
                    }
                },
                resolve: {
                    account:  function($timeout, accountFactory){
                        //Comment
                        return $http({method: 'GET', url: '/account.json'});
                     }
                },
                controller: ['$scope', 'account',  function($scope, account){
                    // You can be sure that promiseObj is ready to use!
                    $scope.data = account;
                    console.log('SCOPE!!!!!');
                }],
            })
            .state('app.accessory', {
                url: 'accessory',
                views: {
                    'content@': {
                        templateUrl: '../views/accessory/listing.html',
                        controller: 'accessoryListingCtrl',
                        controllerAs: 'vm'
                    }
                }

            })
    }]);
3
  • 1
    It has been my experience that, when using multiple views, each needs its own controller and the state's controller does not get run. (As such, you should see the same problem even if you completely removed the resolve). Commented Feb 25, 2015 at 16:17
  • @JAAulde - resolve does not have to be nested under each view. Just tested that this morning. Any resolved variable defined at the 'state' level will be available to 'views'. Commented Jul 9, 2015 at 15:27
  • @RichardClayton I see. I am unfortunately supporting old browsers and have been stuck on AngularJS 1.2.x, so I honestly have no idea what is possible. What version did you use to try it? Commented Jul 9, 2015 at 15:28

2 Answers 2

1

Your parent state config is not correct. When using multiple named views A controller does not belong to a state but to a view, so you should move your controller statement to the specific view declaration, or all of them if you need it everywhere.

See here: https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views

$stateProvider
  .state('report',{
    views: {
      'filters': {
        templateUrl: 'report-filters.html',
        controller: function($scope){ ... controller stuff just for filters view ... }
      },
      'tabledata': {
        templateUrl: 'report-table.html',
        controller: function($scope){ ... controller stuff just for tabledata view ... }
      },
      'graph': {
        templateUrl: 'report-graph.html',
        controller: function($scope){ ... controller stuff just for graph view ... }
      },
    }
  })
Sign up to request clarification or add additional context in comments.

1 Comment

Where is the resolve parameter here, as this is not working for me
0

I don't know why the controller does not get called. But you can start by making sure that resolve always return data.

resolve: {
    account:  function($timeout, accountFactory){
        //Comment
        return $http({method: 'GET', url: '/account.json'})
            .$promise.then(
                function(data) { return data; },
                function(error) { return error; });
    }
}

2 Comments

I am curious should we be avoiding making an $http request in the resolve? Shouldn't that be happening in a service? I am asking because this is what I am trying to solve.
It's always best to abstract this in a service

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.