AngularJS严格DI模式的好处是什么? [英] What is the benefit of AngularJS Strict DI mode?

查看:216
本文介绍了AngularJS严格DI模式的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了AngularJS严格DI模式。目的是什么?使用它的好处吗?我们会特别在移动设备上使用这些功能,可以显着改善性能吗?



我尝试将其应用于我的代码,而在编写代码时没有做任何注释。但是,我的代码是在编译过程中进行简化,并且在编译期间进行注释。但是为什么在将严格的DI模式添加到我的代码之后,我仍然收到错误说需要显式注释?

解决方案

严格的DI模式基本上会抛出错误,当在构建时发现一段不符合缩小的代码;但是请注意,代码可能是正确的,没有逻辑语法错误。



引用文档:


如果该属性存在于应用程序元素上,则注入器将以strict-di模式创建。这意味着应用程序将无法调用不使用显式函数注释(并且因此不适合缩小)的功能,如依赖注入指南中所述,有用的调试信息将有助于跟踪这些错误的根源。


例如,此代码触发错误,因为($ scope,$ http, $过滤器)没有使用 $ inject 或赋予 .controller(A,B)将数组作为第二个字段。

  angular.module(myApp,[])
// BadController不能被调用,因为
//要注入的依赖项不是明确列出的
//。
.controller(BadController,function($ scope,$ http,$ filter){
// ...
});

右片段:

  angular.module(myApp,[])
.controller(GoodController1,GoodController1);

GoodController1。$ inject = [$ scope,$ http,$ filter];

函数GoodController1($ scope,$ http,$ filter){}

或:

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

为了回答您的问题,使用它没有显着的性能提升。它只授予您最小化错误的安全性。这是因为当您使用 $ scope 而不进行显式注释时,会将变量名称更改为破坏代码。


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

解决方案

Strict DI Mode basically throws errors when, at build 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.

这篇关于AngularJS严格DI模式的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆