henriquels
henriquels

Reputation: 546

Reload data after state transition

I have a problem after calling the $state.transitionTo method.

$state.transitionTo('example', {}, { reload: true});

When I access my view in the first time, the data is correctly loaded. If I manually go back in my application and try to access the same page, the data is reloaded. But, when I call the $state.transitionTo, the code inside the controller is not executed and myList is not updated.

Here is the code for the controller.

        .controller('ExampleCtrl', function 
                   ($scope, $state, $stateParams, Service) {

               $scope.myList= {};

               var exampleCtrl = $scope;
               ProdutorService.findAllData($scope.example.idExample).then(function(data){
                      exampleCtrl.myList = data;
               });
         }

I think that it might be related to this bug in the $state.reload(): (bug with controllers reinstantiating right now, fixing soon). https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statereload

Is there any option to enforce executing the controller code or to call a controller function after the state transition?

I am using angularjs with ionic and cordova. It is a mobile application.

Upvotes: 1

Views: 1466

Answers (2)

Aaron Saunders
Aaron Saunders

Reputation: 33345

you can also use the $ionicView.enter listener to update the data, this way the whole view will not need to be recreated.

Scroll Down to View Lifecycle in this blog post

Upvotes: 0

henriquels
henriquels

Reputation: 546

I found the solution.

The responsible for the caching was the ionic framework.

By default, Ionic caches all the views for performance. In order to avoid that, it is needed to add a cache:false in the state definition.

        .state('example', {
            url: "/example",
            templateUrl: 'templates/example.html',
            controller: 'ExampleCtrl',
            cache: false
        })

This way, my data is being refreshed.

Upvotes: 2

Related Questions