zmanc
zmanc

Reputation: 5409

How to properly refresh scope when changing state in ui.router

I am using ui.router to manage the state of some pages on my app. When I change state I would like the controller to reload the objects in scope. However I am not finding a way to tie into state change to do this.

Is there a way I can hook into the state transition and fire a request for my angular resource?

Here are my two state transitions, which are in separate functions.

$state.transitionTo('myObj.current', {myObjId: $scope.myObj.myObjId});

$state.transitionTo('myObj.current.edit', {myObjId: $scope.myObj.myObjId});

When the state changes I would like to call the _getMyObjDetails function.

var   _getMyObjDetails = function (myObjId) {
      var _myObj = MyObjService.get({myObjId: myObjId});
      _myObj.$promise.then(function (data) {
        $scope.myObj = data;
        ngProgress.complete();
      });
      return _myObj;
    };

Upvotes: 0

Views: 1410

Answers (1)

DanielM
DanielM

Reputation: 1195

If I understand you properly, I think the only thing you need to do is listen the $stateChangeStart event. You can inject the $rootScope into your controller and make it like so:

$rootScope.$on('$stateChangeStart', function (event, next) { /* Your code... */ });

Hope that helps.

Upvotes: 1

Related Questions