AngularJS: Injecting a factory / service with identical name but under different module -
let's have 3 factories of same name dserrorlog
each under different module
angular.module('module1').factory('dserrorlog', function () { return { show: false, msg: "" }; }); angular.module('module2').factory('dserrorlog', function () { return { show: false, msg: "" }; }); angular.module('module3').factory('dserrorlog', function () { return { show: false, msg: "" }; });
how inject correct instances correct module 1 e.g. dserrorlog
under module3
controller? suppose syntax such module3.dserrorlog
won't work here.
angular.module('mainapp', ['module1', 'module2', 'module3']) app.controller('mainctrl', function ($scope, dserrorlog) { });
cool question!
it turns out order include them affects 1 get. each module in add overwrite injector. in example dserrorlog module3 if inject normally.
turns out can still injector other modules if want use them. here fiddle show how that: http://jsfiddle.net/7yh7p/
app.controller('myctrl', ['$scope', '$injector', 'test', function($scope, $injector, test) { $scope.injected = test.data; var inj = angular.injector(['mod1']); $scope.my_inject = inj.get('test').data; }]);
the 2 have different values!
hope helped!
Comments
Post a Comment