Angular js - check if current url matches the route (with dynamic url params)

后端 未结 3 410
一生所求
一生所求 2020-12-15 08:11

i\'m simply doing setting this:

app.config([\'$routeProvider\',function($routeProvider) {
  $routeProvider
  .when(\'/asd/:id/:slug\',{
    templateUrl:\'vie         


        
3条回答
  •  悲&欢浪女
    2020-12-15 08:32

    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;
    };
    

提交回复
热议问题