What is the benefit of AngularJS Strict DI mode? What is the benefit of AngularJS Strict DI mode? angularjs angularjs

What is the benefit of AngularJS Strict DI mode?


Strict DI Mode basically throws errors when, at run time, it is found a piece of code that is not compliant to minification; but note that the code may be right and without logical-syntactical errors.

Citing the documentation:

if this attribute is present on the app element, the injector will be created in "strict-di" mode. This means that the application will fail to invoke functions which do not use explicit function annotation (and are thus unsuitable for minification), as described in the Dependency Injection guide, and useful debugging info will assist in tracking down the root of these bugs.

For example this code triggers an error because ($scope, $http, $filter) are not explicitly injected using $inject or giving to the .controller(A,B) method an array as second field.

angular.module("myApp", [])// BadController cannot be invoked, because// the dependencies to be injected are not// explicitly listed..controller("BadController", function($scope, $http, $filter) {  // ...});

Right snippet:

angular.module("myApp", [])  .controller("GoodController1", GoodController1);GoodController1.$inject = ["$scope", "$http", "$filter"];function GoodController1($scope, $http, $filter){}

or:

angular.module("myApp", [])  .controller("GoodController1",               ["$scope", "$http", "$filter", function ($scope, $http, $filter){     //...}]);

In order to answer at your question there is no significant performance improvement by using it. It only grant to you the minifiability error safeness. This because minification changes variables names breaking your code when for example you use $scope without explicit annotation.


You can also add strict-di like this:

 angular.bootstrap(document, ['app'], {        strictDi: true    });

when using angular meteor es6 type applications.


Angular strict DI enforces code minifyability.

When your code is minified the names of the parameters are shortened, which breaks angular's DI. To counter that problem angular has added two(maybe more now) alternative ways to add dependency's.

Perhaps the most common way and the one used by ng-annotate is placing an array instead of an function as the second parameter. The dependency's are the string's before the last element in the array, the string's are the dependency names.

controller.$inject(['$scope']);angular.module('app', ['dependency']).controller('myCtrl', ['myFirstDep',function(willBeInjectedHere){}])

Your ng-annotate is probably not running before angular does it's checks. Make sure you are NOT running uglify together with annotate, do it explicitly BEFORE. If your code is throwing error, then most likely there is somewhere that the annotation was not made.