Angularjs bundles frontend with multiple implementations

I would like to add various interface implementations to the angularjs service, however I cannot find a way to do this. Is there a way to configure the angularjs injector to bind the interface to the implementation? Similar to using other DI frameworks like Ninject or Guice. eg.

var myInterface = function() {
    return {
        func: {}
    };
}

var impl1 = function() {
    return {
        func: function() {
            console.log("implementation 1");  
        }
    };
}

var impl2 = function() {
    return {
        func: function() {
            console.log("implementation 2");  
        }
    };
}

var app = angular.module("app", ["myInterface"]);

app.service("myService1", function(myInterface) {
    myInterface.func();  //should output "implementation 1"
});

app.service("myService2", function(myInterface) {
    myInterface.func();  //should output "implementation 2"
});

//I would like to do something as follows, similar to how one would use Ninject for eg
//Assume myInterface, impl1, impl2 are all angular services
bind("myInterface").to("impl1").wheninjectedinto("myService1");
bind("myInterface").to("impl2").wheninjectedinto("myService2");
      

Run code


+3


source to share





All Articles