11

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?

3
  • Is the URL is outside the scope of the Angularjs project? Can you provide an example? Commented Jan 10, 2014 at 22:41
  • Yes, some URLs are handled by the backend (Play framework). Added some details above. Commented Jan 12, 2014 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 Commented Jan 12, 2014 at 14:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for quick reply! Think otherwise could do the trick, I'll give it a try and let you know.
1

Try using this will get the current sate name.

var stateName = $state.current.name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.