Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find route from angularjs routeprovider by name

I'm defining my routes like this.

$routeProvider
.when('/Home', {
     name: 'Main',
     templateUrl: 'Main.html',
     controller: 'MainController',
     controllerAs: 'ctrl'
})
.when('/About', {
     name: 'About',
     templateUrl: 'About.html',
     controller: 'AboutController',
     controllerAs: 'ctrl'
})

How can I in a controller find the '/About' URL by querying on the name 'About', when I'm in the Main controller?

like image 445
Trylling Avatar asked Dec 12 '22 08:12

Trylling


1 Answers

You can inject $location to your controller and get the URL using $location.path() or inject $route to get the route name.

Example:

function MainCntl($scope,$location,$route) {
  $scope.location = $location.path(); // '/Home'
  $scope.routeName= $route.current.$$route.name; //'Main'
}
like image 136
Alex Choroshin Avatar answered Jan 05 '23 12:01

Alex Choroshin