DeepStateRedirect in angular ui-router 1 – how to reset the deep state?

Issue

I’m using angularJS and migrating to ui-router v1. I’m trying to get deep state redirects working like they used to in the previous version of ui-router.

I’ve successfully implemented the DSRPlugin in my config modules, and deep state redirects are firing and work as expected. However, I’m unable to reset the deep state. I need to be able to reset the deep state on a button click, which means logic within my component. Previously I could inject $deepStateRedirect into my controllers and simply call $deepStateRedirect.reset({}), but I’m no longer able to inject $deepStateRedirect. How can I access the reset method in ui-router v 1?

I have also noticed that when using DSR as a config object you can specify a function to determine if the redirect occurs. I could alternatively use this to determine whether to do the redirect or not, but the documentation is lacking. It shows that I should return a truthy value to do the redirect or a falsey value to prevent the redirect. In testing, returning true or false only causes a transition error: “i.state is not a function”.

I’m not using a build process, just plain script includes.
Anyone have any ideas on how to make this work through either of the above methods?

Solution

This may not be the best practice way of doing the reset, but I found a solution after logging out various ui-router objects.

Inside of your controller you must inject the $uiRouter object. Then, you can set a variable to $uiRouter._plugins[“deep-state-redirect”]. The reset() and other methods are available on the plugin’s prototype.

You can then use that object and call those methods similar to how it worked in the previous version when injecting $deepStateRedirect.

var $deepStateRedirect = $uiRouter._plugins["deep-state-redirect"];
$deepStateRedirect.reset({});

Answered By – mrSpear

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.