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?
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 edited Jan 12, 2014 at 12:00 cnmuc asked Jan 10, 2014 at 22:17 cnmuccnmuc 6,1453 gold badges26 silver badges32 bronze badges 3- Is the URL is outside the scope of the Angularjs project? Can you provide an example? – J.P. Armstrong Commented Jan 10, 2014 at 22:41
- Yes, some URLs are handled by the backend (Play framework). Added some details above. – cnmuc 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 – J.P. Armstrong Commented Jan 12, 2014 at 14:48
2 Answers
Reset to default 1It'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.
Try using this will get the current sate name.
var stateName = $state.current.name;