Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Is the URL is outside the scope of the Angularjs project? Can you provide an example? –  J.P. Armstrong Jan 10 at 22:41
    
Yes, some URLs are handled by the backend (Play framework). Added some details above. –  cnmuc Jan 12 at 11:59
    
There is a routes file you can control who gets what routes. You can setup angularjs at the root of your URL / and redirect all the API calls to play from /api. Angularjs will never have to worry about it. Users shouldn't be given the API urls anyways –  J.P. Armstrong Jan 12 at 14:48
add comment

1 Answer

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.

share|improve this answer
    
Thanks for quick reply! Think otherwise could do the trick, I'll give it a try and let you know. –  cnmuc Jan 12 at 12:03
add comment

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.