本文介绍了Angular js - 检查当前 url 是否与路由匹配(使用动态 url 参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在做这个设置:
app.config(['$routeProvider',function($routeProvider) {
$routeProvider
.when('/asd/:id/:slug',{
templateUrl:'views/home/index.html',
controller:'Home',
publicAccess:true,
sessionAccess:true
});
:id 和 :slug 是动态参数.
现在我想检查当前网址是否匹配
那个路由(/asd/:id/:slug
)
例如网址:asd/5/it-is-a-slug
哪种方法最简单?
我试过这个:
$rootScope.$on('$routeChangeStart', function(){
console.log($route.current);
});
但有时在切换页面路由时它在控制台中什么都不返回
but sometimes it returns nothing in console while switching page routes
推荐答案
当前路由允许您使用正则表达式.注入 $route
服务并探索当前路由对象.即regexp
部分:
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;
};
这篇关于Angular js - 检查当前 url 是否与路由匹配(使用动态 url 参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!