Angular js - 检查当前url是否与路由匹配(使用动态url参数)

its*_*sme 14 javascript match angularjs ngroute

我只是在做这个:

app.config(['$routeProvider',function($routeProvider) {
  $routeProvider
  .when('/asd/:id/:slug',{
    templateUrl:'views/home/index.html',
    controller:'Home',
    publicAccess:true,
    sessionAccess:true
  });
Run Code Online (Sandbox Code Playgroud)

:id和:slug是动态参数.

现在我想要check if current url matches那条路线(/asd/:id/:slug)

例如网址: asd/5/it-is-a-slug

哪种方式最简单?

我试过这个:

$rootScope.$on('$routeChangeStart', function(){ 
   console.log($route.current); 
});
Run Code Online (Sandbox Code Playgroud)

但有时它在切换页面路由时在控制台中不返回任何内容

dfs*_*fsq 25

当前路由允许您使用正则表达式.注入$route服务并探索当前路径对象.即regexp部分:

$route.current.regexp
Run Code Online (Sandbox Code Playgroud)

这是你如何使用它(例如从控制器方法检查当前菜单项(具有动态部分)是否应该被激活):

$scope.isActive = function (path) {
    if ($route.current && $route.current.regexp) {
        return $route.current.regexp.test(path);
    }
    return false;
};
Run Code Online (Sandbox Code Playgroud)