Question

I'm probably just missing something simple, but I want to alias my routes. I am trying to get the route /hello_world to just forward to the existing route of /posttemplate. I want to be able to alias my routes like this to give users and easy to remember way to get to dynamic links... Like Permalinks in blogs. Also, this is sort of a separate question, does anyone know of a good way in angular to define all these routes in a json file or xml or something that will keep this clean as I add many routes to it?

application.config(['$routeProvider', '$locationProvider',  function AppConfig($routeProvider, $locationProvider, $routeParams) {


 $locationProvider.html5Mode(true).hashPrefix('!');

$routeProvider.when("/dashboard", {
    controller: 'controllers.DashboardController',
    templateUrl: 'templates/dashboard.html'

}).when("/wishlist", {
    controller: 'controllers.WishlistController',
    templateUrl: 'templates/wishlist.html'
}).when("/login", {
    controller: 'controllers.LoginController',
    templateUrl: 'templates/login.html'
}).when("/posttemplate", {
    controller: 'controllers.PostTemplateController',
    templateUrl: 'templates/posttemplate.html'
}).when("/hello_world", {
    controller: 'DynamicRouteController',
    templateUrl: function(params){ return '/posttemplate?id=1' }
}).otherwise({
    controller: 'controllers.HomeController',
    templateUrl: 'templates/home.html'

});


}]);
Was it helpful?

Solution

I think you should first plan your URL patterns carefully. It seems not a good idea to have /{{post_name}} and /posttemplate at the same level.

Maybe you could try to add a path prefix to all your individual articles, like /post/:post_name. Then you can just add this to your router config:

.when("/post/:post_name", {
    controller: 'DynamicRouteController',
    templateUrl: 'templates/posttemplate.html'
}

Then in DynamicRouteController, read post_name from $routeParams and map it to your post IDs. You may add something like a unique_name field to your posts, in which stores the URL (/post/post_name) you want it to be accessible from.

Or, if you really want, you can also put them the same level. Just make sure that the "catch-all" item is at the end of your router configs.

And, if you want, you can definitely save the router configs into a JSON file. Just iterate over an array of config items and pass them to when().

OTHER TIPS

You can use redirectTo to redirect from an "alias" to some other route.

.when("/hello_world", {
    redirectTo: '/posttemplate'
}

See the docs on $routeProvider for more info.

Well... Here is what I ended up doing... If anyone knows a better way... just let me know.

routes["/dashboard"] =  { 

     controller: 'controllers.DashboardController', 
     templateUrl: 'templates/dashboard.html', 
     aliases: ['/dash','/dasher'],
     meta: {
       title: "Dashboard",
       description: 'This is my description'
     }

 };

routes["/bucket"] =  {    
             controller: 'controllers.PostTemplateController',  
             templateUrl: 'templates/posttemplate.html', 
             aliases: [],
             arguments: {
                   id: 1
               },
             meta: {
               title: "Dashboard",
               description: 'This is my description'
               }


 };

//Incontroller
     if (!postId)
        postId = $route.current.arguments.id;

Check out this project ui-router. It provides several features to enhance your route and template management.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top