在Angularjs了解编码风格 [英] Understanding coding style in Angularjs

查看:68
本文介绍了在Angularjs了解编码风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了不同的编码风格使用Angularjs,它让我觉得每个编码风格的优势和劣势。

I came across different coding style using Angularjs and it made me think what the advantage and disadvantage of each coding style.

如。
声明控制器:

eg. Declaring of controllers:

风格#1

angular.module('mainCtrl', []);
function MainCrl($scope, $rootScope) {}

风格#2

angular.module('mainCtrl',[])
.controller('MainCtrl', function($scope, $rootScope)) { ... });

样式#3

angular.module('mainCtrl',[])
.controller('MainCtrl', ['$scope', '$rootScope', function(scope, rootScope)) { ... }]);

因此​​风格#3有点像使用别名,不使用别名已当你打算写一个testscript(单元测试)的作用?我只是希望有一个更好的了解和正确的方法使用Angularjs框架时。

Hence style #3 is somewhat like using an alias, does using an alias have an effect when your going to write a testscript (unit testing)?. I just want to have a better understanding and the correct approach when using Angularjs Framework.

请分享您对这个想法。谢谢!

Please do share your thoughts about this. Thanks!

推荐答案

风格#1意味着控制器模块作为全局,不行了小试的项目,但对于任何严肃的工作之外定义的,一切都应该用做#2或#3。之间#2和#3的差别是#3是minifiable如#2的$范围和$ rootScope名称通常被优化了,这会导致应用程序失败。 #3商店这些作为将不精缩出字符串。

Style #1 means the controllers are defined outside of the module as globals, alright for small test projects but for any serious work, everything should be done using #2 or #3. The difference between #2 and #3 is #3 is minifiable as the $scope and $rootScope names in #2 will normally be optimised out, which causes the application to fail. #3 Stores these as strings which will not be minified out.

如果有,至少,你会被污染减量的code可能性,去#3。有一个在使用#1,#个2,所以我倾向于避免#1干脆很少一点。

If there's at least a possibility that you'll be minifying your code, go for #3. There's very little point in using #1 over #2 so I tend to avoid #1 altogether.

这篇关于在Angularjs了解编码风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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