4

Is it possible to add and remove routes after the application is already running?

We have an application, it is already running in the browser, and we dynamically load extra scripts when the user starts a widget in the app. (see it as the app store of google or apple, and you click on an app and it opens the app, with it's own scripts).

Now 1 of those things an app needs to do, is have routes inside the app.

The current routing:

  • app store: /apps
  • app store's app detail: /apps/:appname
  • the app's url: /app/:appname
  • a route within an app: /app/:appname/:... this could be: /app/notes/create or /app/contacts/edit or /app/contacts/new

Each app can have different routes, not necessarily on the same level, they can be /app/news/foreign/latest

Each app has to be separate, and the routes should therefor be loaded in when the app's scripts load. we managed to do this for controllers and directives via the $controllerProvider.register method to add controllers to the application after the application is already started.

But the $routeProvider is not available after the config phase of the app.

I managed to make it public in a dirty way:

var myApp = angular.module('myApp', []);
myApp.config(function AppConfig($routeProvider) {
    myApp.routeProvider = $routeProvider;
});

This way it looks like I can register routes, but as I expected, any of these urls don't work when you enter them manually in the browser, as the angular application itself does not know these routes at run time.

In fact, these routes are only available after the /app/:appname route was triggered which then loads the scripts and adds the extra route.

TL;DR:

  • Is it possible to cleanly register new routes after the app has started
  • Does anyone know a solution to direct linking to one of these subroutes, when they are not known to the application 'yet'?

I'm trying to put a jsfiddle together but it's a hard concept to put in a jsfiddle.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Sander
  • 13,301
  • 15
  • 72
  • 97
  • 2
    See [this answer](http://stackoverflow.com/questions/13153121/how-to-defer-routes-definition-in-angular-js/13173667#13173667) – noj Aug 27 '13 at 14:26
  • sorry for the late reply, I use a similar technique for registering routes after kickoff, but that given answer does not help and does not work with deeplinking. when you link to 1 of those dynamic routes, the app starts and does not know that route, therefor it doesn't work and throws you to the otherwise statement (which in our case is the login page) – Sander Sep 13 '13 at 11:07

1 Answers1

1

Thinking more about this, having solved our problem on a different way, it is just getting clear to me that my request just isn't possible.

  • you cannot link to a route that is not registered yet
  • registering routes afterwards can be done, but not as clean as using the routeProvider in the module's config section.
Sander
  • 13,301
  • 15
  • 72
  • 97