Expand Minimize

Don't inject invalid dependencies in controllers

When injecting dependencies in controllers ensure that they point to valid objects

CheckId NG1010402
TypeName DontInjectInvalidDependenciesInControllers
Severity CriticalError
Type Controller

When injecting dependencies in controllers ensure, that they point to valid objects.

Bad practice (dataservice is incorrectly spelled)

angular
    .module('app')
    .service('dataService', function() {
    })
    .controller('HomeController', function(dataservice) {
        $(".search-details-form").hide();
    });


When injecting depependencies into controllers, also keep in mind that unless you use the inline array notation, your code might break after minification.

The following code snippet:
angular
    .module('app')
    .service('dataService', function() {
    })
    .controller('HomeController', function(dataService) {
        $(".search-details-form").hide();
    });


After minification could become:
angular.module("app").service("dataService",function(){}).controller("HomeController",function(e){$(".search-details-form").hide()});


The dataService parameter has been replaced with e which will cause AngularJS to fail finding the matching dependency to inject. To avoid this issue you should inject dependencies using the inline array notation:

angular
    .module('app')
    .service('dataService', function() {
    })
    .controller('HomeController', ['dataService', function(dataService) {
        $(".search-details-form").hide();
    }]
);

Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.