Add routes and templates dynamically to $routeProvider

9,048 views
Skip to first unread message

Uri Goldshtein

unread,
May 28, 2012, 3:54:31 PM5/28/12
to ang...@googlegroups.com
Hi!
I'm trying to dynamically initialize routes and templates to $routeProvider from a JSON string.
Something like this (I know it looks weird...):

         angular.module('someClient', []).
                  config(['$routeProvider', function($routeProvider) {
              $routeProvider.
            for (jsonRoute as jsonRoutes){
                      when(jsonRoute.url, {template: jsonRoute.temaple,   controller: allRoutesCtrl}).
}
                      otherwise({redirectTo: '/mainRoute'});
          }]);

Is there a conventional way of doing this?
My goal is to take all the static pages in a Jekyll blog and dynamically create routing for them.

I hope iv'e managed to explain myself   :)

Thanks!

Vojta Jína

unread,
Jun 13, 2012, 2:27:46 AM6/13/12
to ang...@googlegroups.com
Well, just do a loop over the collection of pages:

angular.forEach(jsonRoutes, function(r) {
    $routeProvider.when(r.url, {template: r.template, controller: r.controller});
});

You have to inline the jsonRoutes, as you can't get hold of $http to send a request during config.

V.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/M1CpJ-1lz0EJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

jdmo...@gmail.com

unread,
Jun 15, 2012, 6:26:54 PM6/15/12
to ang...@googlegroups.com
Vojta

I am trying to do the same thing as Uri and I had implemented my solution just how you had described to him in your response. The issue is that the config loads before the inline script has finished loading the JSON from our web service(if I put a breakpoint in the code it works great because of the pause)! Do you have any suggestions of how to get around this, is there someway to use a callback with the app module config?

Thanks 

Vojta Jína

unread,
Jun 26, 2012, 1:12:48 AM6/26/12
to ang...@googlegroups.com
Hmm, can't you include the json file with the routes, instead of doing a jsonp ? That would be better.

<script src="angular.js">
<script src="myApp.js">
<script src="routes.js">

If you really want to load the routes asynchronously, you would have to some hack, say, something like this:
var nastyGlobalReferenceToRouteProvider;
appModule.config(function($routeProvider) {
  nastyGlobalReferenceToRouteProvider = $routeProvider;
});

// later, when you get the routes, call nastyGlobalReferenceToRouteProvider.when(...)

That should work, you would probably have to do $route.reload() after adding the routes, otherwise $route would miss the initial route.


V.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/iaMUFZ0x_9AJ.

Pepa Štefan

unread,
Oct 31, 2012, 3:52:38 AM10/31/12
to ang...@googlegroups.com
Vojta, is it still the same?
I need to configure routes later as well (after login), see http://stackoverflow.com/questions/13153121/how-to-defer-routes-definition-in-angular-js . Or is there any better approach in newer versions?
Reply all
Reply to author
Forward
0 new messages