为什么在 AngularJS 中定义控制器时不使用显式注释? [英] Why wouldn't you use explicit annotations when defining controllers in AngularJS?

查看:20
本文介绍了为什么在 AngularJS 中定义控制器时不使用显式注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手,正在学习编写控制器函数的两种风格.似乎有人不使用显式注释的唯一原因是为了节省时间,这似乎不是一个很好的理由.能够缩小/混淆代码似乎是我希望保留在任何应用程序中的要求.

I am new to AngularJS and learning about the two styles of writing controller functions. It seems as though the only reason someone would not use explicit annotations is to save time, which doesn't seem like a good reason. And being able to minify/obfuscate code seems like a requirement I would want to keep in any application.

另请注意,我不是在问哪个更好或要求进行辩论.我问的是出于什么原因(或在什么情况下)使用显式注释会更有益.

Also note that I am not asking which is better or asking for a debate. I am asking for what reasons (or in what situation) it would be more beneficial to not use explicit annotations.

我正在谈论的示例:

module('myApp').controller('MyController', function($scope) {});

对比

module('myApp').controller('MyController', ['$scope', function($scope) {}]);

推荐答案

内联数组注释 只是对 Javascript 限制的一种解决方法,以允许缩小 Angular 代码而不是停止工作.但这不是一个很好的解决方案,因为如果强迫您复制您的代码.我们都知道重复代码有多糟糕.Angular 文档本身承认:

The inline array annotation is simply a workaround over Javascript limitations to allow Angular code to be minified and not to stop working. But it isn't a great solution because if forces you to duplicate your code. And we all know how bad duplicate code is. Angular documentation itself acknowledges that:

使用此类注解时,注意保留注解数组与函数声明中的参数同步.

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration.

添加新的依赖项而忘记添加相应的注释太容易了.或者重新排序参数而忘记更新注释列表.相信我.去过也做过.

It's way too easy to add a new dependency and forget to add the corresponding annotation. Or to reorder the arguments and forget to update the list of annotations. Trust me. Been there, done that.

幸运的是,有一些由聪明人开发的工具可以通过自动注释代码来减轻我们的负担.可能最著名的是 ng-annotate,正如@pankajparkar 所提到的.您所要做的就是将其插入到您的构建过程中,您的代码就会得到正确的注释.

Fortunately there are tools developed by smart people that take that burden off our shoulders by taking care of annotating the code automatically. Probably the most known is ng-annotate, as mentioned by @pankajparkar. All you have to do is plug it into your build process and your code will be correctly annotated.

老实说,我觉得 Angular 文档建议不要遵循这种方法真的很奇怪.

To be honest, I find it really odd that Angular documentation recommends against following this approach.

这篇关于为什么在 AngularJS 中定义控制器时不使用显式注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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