AgmLauncher
AgmLauncher

Reputation: 7270

How do you change dependencies at runtime in AngularJS?

Say I have the following controller:

var MyController = function(dataSource) {
    this.something = dataSource.getSomething();
}

But I want to inject a different implementation of dataSource depending on whether or not I'm online or offline (e.g. LocalStorageDataSource and APIDataSource).

What is the mechanism in Angular that would let me do that both on script startup, and also when an offline/online event fires?

Basically I'm trying to achieve polymorphic dependency injection at runtime in Angular.

Upvotes: 1

Views: 150

Answers (1)

VRPF
VRPF

Reputation: 3118

Here is an example from TodoMVC Angular:

https://github.com/tastejs/todomvc/blob/gh-pages/examples/angularjs/js/services/todoStorage.js

Upvotes: 1

Related Questions