app.config(['$controllerProvider','$compileProvider', '$filterProvider', '$provide',function($controllerProvider,
$compileProvider, $filterProvider, $provide) {
app.register = {
controller: $controllerProvider.register,
directive: $compileProvider.directive,
filter: $filterProvider.register,
factory: $provide.factory,
service: $provide.service
};
}
]);
app.controller('myCntrl', ['$scope', function($s) {
app.register.factory('customFactory',[customFactory]);
customFactory.init();
});
factory.js
function customFactory(){
var c= {};
c.init = function(){
console.log('init function');
}
return c;
}
Here I will be adding factory.js file in runtime and I now need to inject custom factory in my controller. So I have used $provide.factory method for it. But, I am not able to call init() function inside customFactory
Inside the controller that dynamically registers
customFactory, thecustomFactoryname binding is just a function. To callinit()at the location you've noted, you'll need to invokecustomFactoryas a function.However, if you've got another recipe elsewhere (e.g. controller) which needs to depend on
customFactory, then you can accessinit()as you might expect.When
customFactorygets registered, the AngularJS dependency injector knows how to create it as a singleton and subsequently you can just access it as a value.