How to $inject dynamically dependence in a controller

Don’t call angular.injector() — this creates a new injector. Instead, inject the already-created $injector into your controller and use it:

So instead of:

var algoController = function($scope) {
    $scope.base64 = angular.injector(['main']).get('base64');

Do this:

var algoController = function($scope, $injector) {
    $scope.base64 = $injector.get('base64');

But most of the time you should inject your service directly, rather than dynamically, like so:

var algoController = function($scope, base64) {
    $scope.base64 = base64;

See also AngularJS dynamically inject scope or controller

Leave a Comment