我定义了一些路线:
angular.module('myApp', [])
.config('$routeProvider', function($routeProvider) {
$routeProvider.when('/aaa', { templateUrl: '/111.html' })
.when('/bbb', { templateUrl: '/222.html'});
});
Run Code Online (Sandbox Code Playgroud)
我想在用户更改路线时获取路线名称:
angular.module('myApp')
.run(['$rootScope', function($rootScope) {
$rootScope.$on('$routeChangeSuccess', function(scope, current, pre) {
// how to get current route name, e.g. /aaa or /bbb
console.log('Current route name: ' + ???);
}
}]);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何得到它.我可以得到templateUrl,但不是路线名称.
UPDATE
一个更复杂的用例:
$routeProvider.when('/users/:id', { templateUrl: '/show_user.html' })
Run Code Online (Sandbox Code Playgroud)
如果当前路径是:
/users/12345
Run Code Online (Sandbox Code Playgroud)
它应匹配/users/:id,但我如何知道哪条路线匹配并获得路线名称/users/:id?
我只是在做这个:
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)
但有时它在切换页面路由时在控制台中不返回任何内容