0

I'm new in AngularJS Community and I'm developping my first app with this Framework.

I created a new controller with this code :

.controller('AccountCtrl', function($scope, $location) {
alert('test');
})

And my route :

.state('app.account', {
    url: "/account",
    views: {
      'menuContent': {
        templateUrl: "templates/account.html",
        controller: 'AccountCtrl'
      }
    }
  })

The alert popup is shown the first time I access to the controller. But, if I change URL and I come back to AccountCtrl (with a classic html a), the alert popup is not shown again.

Could somebody explain to me why ?

Thanx for your help !

3
  • That' strange, normally the controller will be instantiated every time the url changes. Are you developing with Ionic framework? In Ionic the views and controllers are cashed. You could add a listerner to the scope '$scope.$on('$ionicView.beforeEnter', myUpdateFunction'. Commented Apr 1, 2015 at 9:02
  • Hi Stefan, yes I'm using Ionic Framework. Where I have to add this listener ? In my controller ? Commented Apr 1, 2015 at 9:12
  • I add an example as answer to your question. Hope it helps Commented Apr 1, 2015 at 10:30

2 Answers 2

1

In Ionic Framework views and controllers will be cached by default. You ma add a listener to the views scope to receive a notification when the view is re-active again. For more information see: http://ionicframework.com/docs/api/directive/ionView/ and http://ionicframework.com/docs/api/directive/ionNavView/

You may also disable the cache on a view <ion-view cache-view="false">

.controller('AccountCtrl', function($scope, $location) {
    $scope.$on('$ionicView.beforeEnter', function () {
            // update campaigns everytime the view becomes active
            // (on first time added to DOM and after the view becomes active after cached
        alert('test');
    });
})`
Sign up to request clarification or add additional context in comments.

Comments

1

to reload Controller each time in ui router, use reload: true option on the .state

$stateProvider
.state('app.account', {
      url: "/account",
      reload: true //forcefully reload route and load controller again
})

2 Comments

Hi AB, your answer doesn't work for me :/ Is it because i'm using Ionic Framework ?
reload is whether as state should be rebuild after a param change angular-ui.github.io/ui-router/site/#/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.