Recently I come across with AngularJS Strict DI mode. What is the purpose & benefit of using it? Will we gain significant performance improvement by using it especially on mobile devices?
I try to apply it to my code and I did not do any annotation when writing the code. However, I have my code to be minify, and ng-annotate during build. But why is that after I add Strict DI mode to my code I still get the error saying "Explicit annotation required"?
Good practice is using
strict-di
. App must fail to run when invoke functions that don’t use explicit function annotation. This means that the methods used must be declared. Using theng-strict-di
will ensure that app is confirming with dependency injection guideline and will fail to run if not.You can achieve that by using
ng-strict-di
:See developer guide: https://docs.angularjs.org/guide/di
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:
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.Right snippet:
or:
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:
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.
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.