Javascript 如何在$routeProvider中捕获特定url

Javascript 如何在$routeProvider中捕获特定url,javascript,angularjs,Javascript,Angularjs,我要捕获的URL是 我需要在$routeProvider中捕获上述url。 我的密码是 $routeProvider.when('/:code/',{ template: '', controller: function ($location,$rootScope) { console.log("I got you. .. ");

我要捕获的URL是

我需要在$routeProvider中捕获上述url。 我的密码是

$routeProvider.when('/:code/',{
                    template: '',
                    controller: function ($location,$rootScope) {
                        console.log("I got you. .. ");
                    }
                })
                .otherwise({
                    redirectTo: '/login'
                });
我的问题是我无法在中捕获此url


感谢您的帮助。

使用
$routeParam

$routeProvider.when('/:code/',{
                template: '',
                controller: function ($location,$rootScope) {
                   var urlCopied=$location.path(); // get the current path                     
                }
            })
            .otherwise({
                redirectTo: '/login'
            });
希望对你有所帮助你可以试试这个

//给定的urlhttp://example.com/#/some/path?foo=bar&baz=xoxo
var absUrl=$location.absUrl();
// => "http://example.com/#/some/path?foo=bar&baz=xoxo“


有两种方法可以满足您的需求:

  • 如果您想捕获一个特殊的参数,不要将它放在查询参数中。使用如下url:

    $routeProvider.when('/code/codeID',{
                    template: '',
                    controller: function ($routeParams) {
                        console.log($routeParams.codeID);
                    }
                })
                .otherwise({
                    redirectTo: '/login'
                });
    

  • 然后像这样抓住它:

    $routeProvider.when('/code/codeID',{
                    template: '',
                    controller: function ($routeParams) {
                        console.log($routeParams.codeID);
                    }
                })
                .otherwise({
                    redirectTo: '/login'
                });
    
  • 如果要使用查询参数,可以按如下方式执行:

    $routeProvider.when('/',{
            template: '',
            controller: function ($location) {
               var queryParams = $location.search();
               console.log(queryParams.code);                   
            }
        })
        .otherwise({
            redirectTo: '/login'
        });
    
  • 并使用此URL:

    http://test.sample.com/?code=4/CAKREYsOD89sg7eXahK93Pw7pnMb5ndkiCrzSUlMS1U
    

    我希望这段代码能帮助你

    我需要得到上面的网址被捕获。这与解析url无关。抱歉@Explore-X,编辑了答案检查是否有用。这与$routeProvider有关。当(“/:code/”时,此通配符上未捕获url。我认为这是因为“?”重试