Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of AngularJS Strict DI mode?

Tags:

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"?

like image 636
user1995781 Avatar asked Nov 03 '15 07:11

user1995781


People also ask

What does strict dependency injection do?

Using Strict Dependency Injection Strict mode throws an error whenever a service tries to use implicit annotations. Consider this module, which includes a willBreak service that uses implicit DI: angular. module('myApp', []) .

What are the advantages of dependency injection in AngularJS?

It relieves a component from locating the dependency and makes dependencies configurable. It also helps in making components reusable, maintainable and testable. AngularJS provides a supreme Dependency Injection mechanism. It provides following core components which can be injected into each other as dependencies.

What is AngularJS DI?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider. factory. value.

What is $rootScope in AngularJS?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.


1 Answers

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.

like image 163
morels Avatar answered Oct 08 '22 05:10

morels