定义AngularJS Controller时使用数组表示法的原因 [英] Reason for using array notation when defining AngularJS Controller

查看:138
本文介绍了定义AngularJS Controller时使用数组表示法的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题听起来太明显,请道歉。



我最近开始探索和学习AngularJS。我已经完成了一些很好的教程 -





..还有一些我见过的。



我不是说我已阅读/研究过所有文件。



问题从这里开始 -



现在,提出问题,我发现控制器的定义在一个地方是不同的,而在其他地方则不同 -



一个定义使用一种数组符号(不确定正式术语)进行注射:

  app.controller (MyCtrl,['$ scope',函数($ scope){
$ scope.someData =数组符号;
}]);

还有这个,没有数组:

  app.controller(MyCtrl,函数($ scope){
$ scope.someData =非数组符号;
});

不是说这是我唯一想要了解但是,我是我一定很想了解其中的区别。



这两者之间是否存在重大差异?



<非常感谢。



注意:我确实在SO中搜索了类似的问题,但找不到我想要的内容。抱歉。

解决方案

不同之处在于当第二个缩小时,参数名称将缩小并且角度将为不再能够检查参数以确定要注入哪些依赖项。具有字符串中的依赖性的数组语法意味着它是最小安全的。



有一个名为 ng-annotate 的库,它会将第二个例子改成第一个例子,以便代码再次缩小安全。


Apologies if this question sounds too obvious.

I've recently starting exploring and learning AngularJS. I've gone through some good tutorials -

.. and there are a few other that I've seen.

I'm not saying that I've read/studied all the documents.

Question starts here -

Now, coming to the question, I see that the definition of a Controller is different in one place and it's different in some other -

One definition uses a sort of array notation (not sure of the official term) for injection:

app.controller("MyCtrl", ['$scope', function($scope){
    $scope.someData = "Array notation";
}]);

And there's this, with no array:

app.controller("MyCtrl", function($scope){
    $scope.someData = "non-array notation";
});

Not saying this is the only thing that I'm trying to understand but yes, I'd definitely love to understand the difference.

Is there a major difference between the two?

Thanks a lot.

Note: I did search for similar questions in SO but couldn't find what I was looking for. Sorry.

解决方案

The difference is that when the second is minified, the parameter name will be minified and angular will no longer be able to inspect the arguments to figure out which dependencies to inject. The array syntax with the dependency in a string means that it is minification safe.

There is a library called ng-annotate which will change the second example into the first example so that the code is again minification safe.

这篇关于定义AngularJS Controller时使用数组表示法的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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