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. HTML5_布局and音视频

    HTML5_布局and音视频 I.HTML5标签的改变1.文档声明HTML语法是不区分大小写的HTML5的DTD声明为:<!doctype html>确保浏览器能在HTML5的标准模式下进 ...

  2. Python异常记录

    1.常用异常名 AttributeError 调用不存在的方法引发的异常. EOFError 遇到文件末尾引发的异常. ImportError 导入模块出错引发的异常. IndexError 列表越界 ...

  3. Linux下的GitHub安装与简单配置教程

    1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与使用 在ubuntu下可以使用如下命令进行查看系统 ...

  4. Ubuntu关闭图形界面

    方法一 sudo /etc/init.d/lightdm stop 方法二 init 3 关闭图形界面 init 5 开启图形界面

  5. Java NIO 操作总结

    问题: 1.Java NIO 出现大量CLOSE_WAIT或TIME_WAIT的端口无法释放 CLOSE_WAIT: 参考:http://my.oschina.net/geecoodeer/blog/ ...

  6. HDU1243:反恐训练营

    题目链接:反恐训练营 题意:本质上是求最大公共子序列,然后加上一个权值 分析:见代码 //公共子序列问题 //dp[i][j]表示前s1的前i个与s的前j个匹配得到的最大公共子序列 #include& ...

  7. JavaScript的function对象

    我必须先说Java与JavaScript没有关系,不是我以前想的那个样子的(JavaScript是Java的一种超进化) 在JavaScript中,函数(function)就是对象. JavaScri ...

  8. gem 相关命令

    gem #查看gem源 gem sources # 删除默认的gem源 gem sources --remove http://rubygems.org/ # 增加taobao作为gem源 gem s ...

  9. Lync激活用户遇到ConstraintViolationNoLeadingOrTrailingWhitespace错误

    启用用户的时候出现错误ConstraintViolationNoLeadingOrTrailingWhitespace,如下图 解决方案:域控中,该用户的名字最后多出了个空格,批量生成域用户的脚本问题 ...

  10. [cocos2d-js]长按按钮事件

    定义两个全局变量 var bLeftButtonClick = false; var bRightButtonClick = false; var MainLayer = cc.Layer.exten ...