Ratata Tata
Ratata Tata

Reputation: 2879

angularjs run function every time the page is shown

I have this code:

.controller('HomeCtrl', function( $scope, $http )
{
    $http.get( onlinePath + "/status" ).then(function(result)
    {
        if( result.data.status == true )
        {
            $scope.currentStatus = result.data.userStatus;
        }
    });
});

And when this page is loaded (the first time it opens) it gets online status and shows that in this page.

The thing is, I navigate the app and when I come to this page (I think it is already loaded) it didn't run the $http.get again.

How do I get to know when the page is opened again (not loaded)?

Upvotes: 1

Views: 1321

Answers (2)

S.Galarneau
S.Galarneau

Reputation: 2234

In your routes config you can add the cache : false param, like this for example

.state('app.home', {
    cache: false,
    url: '/home',
    views: {
        'menuContent': {
            templateUrl: 'templates/home.html',
            controller: 'HomeCtrl'
        }
    }
})

Upvotes: 2

sirrocco
sirrocco

Reputation: 8055

Have a look at: http://ionicframework.com/docs/api/directive/ionView/ .

Specifically to the "View LifeCycle and Events" section - there it says that the view is cached (a cache that you can configure) and you can listen to some events for specific .. stages.

So you could do:

  $scope.$on("$ionicView.beforeEnter", function() {
    console.log("http call here");        
  });

Upvotes: 2

Related Questions