What is the benefit of AngularJS Strict DI mode?

前端 未结 4 1399
小鲜肉
小鲜肉 2020-12-31 02:05

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

4条回答
  •  情深已故
    2020-12-31 02:48

    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.

提交回复
热议问题