角JS - 检查当前的URL路径相匹配(动态网址参数) [英] Angular js - check if current url matches the route (with dynamic url params)

查看:202
本文介绍了角JS - 检查当前的URL路径相匹配(动态网址参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是做这个的设置:

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

:ID和:蛞蝓是动态PARAMS

现在我想检查,如果当前URL匹配这条路线( / ASD /:ID /:蛞蝓

Now i would like to check if current url matches that route (/asd/:id/:slug)

例如网址: ASD / 5 / IT-IS-A-塞

这是最简单的方法是什么?

which is the simplest way?

我trye​​d这一点:

i tryed this :

$rootScope.$on('$routeChangeStart', function(){ 
   console.log($route.current); 
});

但有时在控制台切换时的路线页面返回任何

but sometimes it returns nothing in console while switching page routes

推荐答案

当前路线,您可以使用常规的前pression。注 $路线服务,并探索当前路由对象。即正则表达式部分:

Current route allows you to use regular expression. Inject $route service and explore current route object. Namely regexp part:

$route.current.regexp

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

This is how you can use it (example from controller method to check if current menu item (which has dynamic parts) should be activated):

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

这篇关于角JS - 检查当前的URL路径相匹配(动态网址参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆