Module definitions


Angular modules can be declared in various ways, either stored in a variable or using the getter syntax. Use the getter syntax at all times (angular recommended).

Bad:

var app = angular.module('app', []);
app.controller();
app.factory();

Good:

angular
.module('app', [])
.controller()
.factory();

From these modules we can pass in function references.

Module method functions


Angular modules have a lot of methods, such as controllerfactorydirectiveservice and more. There are many syntaxes for these modules when it comes to dependency injection and formatting your code. Use a named function definition and pass it into the relevant module method, this aids in stack traces as functions aren't anonymous (this could be solved by naming the anonymous function but this method is far cleaner).

Bad:

var app = angular.module('app', []);
app.controller('MyCtrl', function () { });

Good:

function MainCtrl () {

}
angular
.module('app', [])
.controller('MainCtrl', MainCtrl);

Define a module once using angular.module('app', []) setter, then use the angular.module('app')getter elsewhere (such as other files).

To avoid polluting the global namespace, wrap all your functions during compilation/concatenation inside an IIFE which will produce something like this:

Best:

(function () {
angular.module('app', []); // MainCtrl.js
function MainCtrl () { } angular
.module('app')
.controller('MainCtrl', MainCtrl); // AnotherCtrl.js
function AnotherCtrl () { } angular
.module('app')
.controller('AnotherCtrl', AnotherCtrl); // and so on... })();

[AngularJS] Best Practise - Module的更多相关文章

  1. 33.AngularJS 应用 angular.module定义应用 angular.controller控制应用

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 模块(Module) 定义了 AngularJS 应用. AngularJS 控制器(Co ...

  2. 淡淡理解下AngularJS中的module

    在AngularJS中module是一个核心的存在,包括了很多方面,比如controller, config, service, factory, directive, constant, 等等. 在 ...

  3. [AngularJS] Best Practise - Minification and annotation

    Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...

  4. [AngularJS] Best Practise - Controller

    ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...

  5. AngularJS - 插件,module注入

    Index.html <body> <div ng-app="myApp"> <div ng-controller="firstContro ...

  6. AngularJS - contorller app module

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. [AngularJS] Best Practise - Resolve promises in router, defer controllers

    See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ /** * Created by Answer12 ...

  8. AngularJs学习

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. 带你走近AngularJS - 基本功能介绍

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...

随机推荐

  1. 实现3D摄像机缓冲系统的一些思考

    最近需要模拟红侠乔伊的镜头运用.这东西初看简单,实际还是很需要功夫的.关键不是程序技术如何(就一个摄像机),而是分析其轨迹和追踪点规律.其实就是一个3D空间中的缓冲系统.你如何确定都有什么参数,这么多 ...

  2. webstorm启动bug

    场景描述: win10系统下,webstorm(32位)经常遇到无法启动的情况. 解决方案: 重启电脑. 1.win10系统需要更新时,webstorm无法启动,此为win10 bug,重启时,系统自 ...

  3. Hadoop 2 初探

    Hadoop 2.6.0的安装略复杂,在一台既有Hadoop 1又有Hadoop 2的server上,要设置好环境变量,必要时候echo $HADOOP_HOME一下看运行的是哪个版本. Master ...

  4. Flex通信-与Java实现Socket通信实例

    Flex通信-与Java实现Socket通信实例  转自:http://blessht.iteye.com/blog/1136888 博客分类: Flex 环境准备 [服务器端] JDK1.6,“ja ...

  5. 远控软件VNC攻击案例研究

    欢迎大家给我投票: http://2010blog.51cto.com/350944           本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  6. 第二百三十一天 how can I 坚持

    哎,蛋疼的一天,一点破问题搞了一下午,还没搞利索. 他们要组织出去玩,我没有参加啊,随便找了个借口. 博客园的字体怎么变小了呢,看着好难受啊,昨天传照片传的? 睡觉.外边下着雨呢,喜欢下雨的夏天还有下 ...

  7. Cocos2d-x单机游戏防八门神器修改数据

    来源:http://cocos2d.9tech.cn/news/2014/0212/39812.html 网上的cocos2d-x教程多为知识点的讲解,但我们学习cocos2d-x的目的是为了什么?为 ...

  8. Mcafee两个Mac版本之间的区别

    近期打算为Mac安装个杀毒软件,由于自己windows平台下用的是VSE,所以Mac平台也首选Mcafee家的东西了.到Mcafee官网下载点一看,有以下几个版本可以用在Mac上: 有点懵了,查看了一 ...

  9. POJ3253Fence Repair(优先队列或单调队列)

    http://poj.org/problem?id=3253 经典题目了,大意是说如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 无论是以下哪种方法,最原始的思路 ...

  10. HDU1963Investment(DP)

    简单DP,题解见代码