Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting state from URL string with Angular UI Router

I'm using state-based routing (Angular UI Router v0.2.7) in a project and looking for a way to get the current state (name) from a given URL string.

Something like:

$state.get([urlString]) returns stateName:String or state:Object

I need this method to check if a state exists to a given URL because not all URLs are mapped to a state in my project. Using Play Framework as backend, some URLs (e.g., login form) are not mapped to a state because they using different templates then the Angular (main) part of my application. For those "none-Angular" pages (i.e., not covered by a state) I would do a reload. To identify URLs not covered by a state I need the method mentioned above. Planned to do it like this:

$rootScope.$watch(function() { return $location.path(); }, function(newUrl, oldUrl) {
    if(newUrl !== oldUrl) {
        if (!$state.get(newUrl)) {
            $window.location.assign(newValue);
        }
    }
}

Already checked the docu but there is no such method.

Any ideas?

like image 501
cnmuc Avatar asked Jan 10 '14 22:01

cnmuc


1 Answers

It's all or nothing. If you plan to use ui-router make sure all your URLs resolve to a state. If the state doesn't exist it will go to the otherwise state. It's possible to have optional parameters.

An alternative is to use .htaccess redirects to catch the URL and redirect you before it hits the ui-router.

Provide more details and we can see what the best option is.

like image 190
J.P. Armstrong Avatar answered Sep 30 '22 01:09

J.P. Armstrong