SimplGy SimplGy - 1 month ago 11x
AngularJS Question

Remove an already-registered $state

I was surprised to learn that ui-router shares state across modules.

That's ok, but for our development environment I'd like to clear all the application states and start from scratch.

It's not convenient to avoid defining them in the first case just for our test environment, because the state definition is mixed in with all the other application loading, and we'd like to be sure that the application dependencies are describe the same way in all environments.

So can I clear, or remove one-by-one, the already defined states?

.config( function($stateProvider) {
// TODO: get rid of $state definitions from the app, we don't want them here


This is not possible. ui-router uses ng-router and ng-router does not expose the routes until after the provider is compiled. Meaning you wouldn't be able to remove states/routes until the app is configured. Which I don't think they will accept a PR.


In response to the comment made. The routes objects used by the $routerProvider is available and you can remove routes.

delete $route.routes['/'];

But the states object used by the $stateProvider is not available.

So ui-router would need to be modified to be able to remove states after config. OR ng-router would need to be modified to be able to remove routes during config.