angularjsroute-provider

Angular RouteProvider, returning a certain template depending on routeParams


I'd like to achieve something like:

.when('/event/group/:gid', {
            if($routeParams.gid == 124){
                controller: 'ACtrl',
                templateUrl: '/event/A/foo.html'
            } else {
                controller: 'BCtrl',
                templateUrl: '/event/B/bar.html'
            }
        })

So literally I would like to use a route parameter as a condition to decide which controller and view to render.


Solution

  • This can be dome by returning a function on your templateUrl.

    .when('/event/group/:gid', {
        controller: 'ACtrl',
        templateUrl: function (params) {
            if(params.gid == 124){
               return '/event/A/foo.html'
            } else {
               return '/event/B/bar.html'
            }
         }
      });