Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In an AngularJS 1.5.8 app, I am using ui-router to navigate between pages which all use the same controller. The ng-controller attribute is applied to the body tag, and the ui-view attribute is applied to a div which is obviously inside the body of the HTML. For simplicity sake, let's say it looks like this:

<body ng-controller="SecurityController as securityCtrl">
    <div ui-view></div>
</body>

An example route, in case that helps to visualize what I am saying, is:

.state('login', {
    url: "/login/:returnState",
    templateUrl: "Account/Login"
})

The routing is working fine, and when I change states it is not re-initializing the controller, which is what I want. I have an errorMessage variable in the controller which I would like to set to undefined whenever the state changes, but I'd rather not add javascript to the navigation buttons to make that happen.

I know that you can register event handlers for routing, and I am successfully using them for $stateChangeError and $viewContentLoaded, but the parameters which are available are event, toState, toParams, fromState, fromParams, and error. I don't see how I could easily use these, without a big switch statement, to tell the common controller to set its errorMessage variable to undefined. Not all routes need to clear out this variable, but I suppose I could just clear it whether doing so is necessary or not.

Also, I heard somewhere that you can make the controller re-initialize after each state change. That is plan B or C.

What is the simplest way of marking a subset of the routes in a way that I could easily determine whether the fromState or toState is marked in this way, so that I can clear the variable? If you can think of a better (still DRY) way to do this, I'm all ears.

share|improve this question
up vote 2 down vote accepted
.state('someState', {
    url: "/somestate",
    data: { resetMessage: true } 
    ...
})

And in your .run

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
  if (toState.data.resetMessage)
  {
     ... //Clear errorMessageVariable
  }
});
share|improve this answer
    
It works like a charm. – Kent Weigel 23 hours ago
    
If someone has this same problem, I should say that, although this answer is a good way to mark routes, it may not allow what the title of the question asks for. I'm not sure if it is possible to clear a controller variable using this technique. If you add $injector to the angular.module(...).run arguments, then you can get a controller of the type that you request, but it's not the same instance that you are already using. For me, that doesn't work. The suggestion in other posts is to put the variable that you want to clear in the service, rather than the controller. That worked for me. – Kent Weigel 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.