最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting state from URL string with Angular UI Router - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 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.

Try using this will get the current sate name.

var stateName = $state.current.name;
发布评论

评论列表(0)

  1. 暂无评论