Sander
Sander

Reputation: 13431

Angularjs: define a route after application config has passed

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:

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:

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

Upvotes: 4

Views: 1334

Answers (1)

Sander
Sander

Reputation: 13431

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.

Upvotes: 1

Related Questions