4

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?

Trylling
  • 213
  • 2
  • 4
  • 9

4 Answers4

7

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'
}
Alex Choroshin
  • 6,177
  • 2
  • 28
  • 36
  • That will give the current location, I need to get the URL for a specific page - without repeating the string value – Trylling Jan 30 '14 at 09:26
  • Sorry for my unclear question, I've updated it now. If I'm on the 'Home' page - how do I get the Url for the 'About' page? – Trylling Jan 30 '14 at 09:38
7

Since there is nothing built in, an extending of route can solve this problem in a nice way:

$provide.decorator('$route', ($delegate) => {

    $delegate.getRoute = (name) => {
        var result = null;
        angular.forEach($delegate.routes, (config, route) => {
            if (config.name === name) {
                result = route;
            }
        });
        return result;
    };

    return $delegate;
});
Trylling
  • 213
  • 2
  • 4
  • 9
  • is `=>` a shorthand notation for `function(..)` or is that provided by a framework/interpreter atop javascript ? – okigan Apr 23 '14 at 05:50
  • Just for the record, this is not CoffeeScript, it's [ES6 Harmony](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). – Koen Nov 06 '14 at 15:32
0

Check the questions:

My solution to this was the following:

Object.getPrototypeOf($route.current) === $route.routes['/Home'];

Given that the route object might be empty after you instantiate your controller, you should use the $routeChangeSuccess event, like this:

$rootScope.$on('$routeChangeSuccess', function(e, curr, prev) {
    if (Object.getPrototypeOf($route.current) === $route.routes['/Home']) {
         // do something when route changes to /Home
    }

    else if (Object.getPrototypeOf($route.current) === $route.routes['/About']) {
         // do something when route changes to /About
    }
}

There's no need for declaring additional properties (although it would be nice if angular allowed it). You just use the pattern string.

As I've said in those other 2 questions, this is behavior is not documented (both $route.current and $route.routes are documented, but the prototype inheritance between them is not), so use it at your own risk.

Also this works on IE9+.

Community
  • 1
  • 1
Tiborg
  • 2,304
  • 2
  • 26
  • 33
-1

If your purpose is to switch url path, you can try this:

function MainCntl($scope, $location) {

    // put this at where your want to find the '/About' URL
    $location.path('/About'); 

}
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
  • 1
    I don't want to repeat the '/About' string, I wan't to get it from $routeProvider based on its name – Trylling Jan 30 '14 at 15:53