Skip to content

Not possible to add state.resolve from $stateChangeStart event #1165

@facultymatt

Description

@facultymatt

Overview:
Sometimes it's desirable to add state resolves on the fly, instead of needing to add them to all states beforehand. For example, a resolve that verifies a user is authenticated or fetches the user from an API.

It's possible to do this with $routeChangeSuccess using the following code:

// @see http://stackoverflow.com/a/18245378/1738217 function authenticate() { if ( user.isAuthenticated ) { return; } // simulate deferred return $timeout(function() { user.isAuthenticated = true; }, 3000); } $rootScope.$on( "$routeChangeStart", function( e, next ) { next.resolve = angular.extend( next.resolve || {}, { __authenticating__: authenticate }); }); 

However the same is not possible with $stateChangeSuccess. This is because the event is broadcast with to.self, see this line. This means if you add a resolve in the $stateChangeSuccess event, you are actually adding on to.self.resolve which is never considered for resolve.

_Related:_
#1153

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions