I would like to dynamically inject some dependencies within a controller. I can do it easily with the $injector like this:
var app = angular.module('app', []);
app.service('serv1', function() {
var me = this;
this.welcome = function(str) {
console.log("Welcome " + str + "!");
};
});
app.controller('Ctrl', function($scope, $injector) {
var servMe = $injector.get('serv1');
$scope.greeting = function() {
servMe.welcome('Master Obi-wan');
};
});
But, let's say that I have a huge service serv1 and do not want to 'bloat' my page if not required. I would like to do it from within my controller (Plunker provided: http://plnkr.co/edit/Szs4Pg?p=preview)
The error I am facing is that the $injector does not seem to know about the newly loaded service. Should I refresh its cache or something? Should I instantiate a new one?
Many thanks in advance.