[AngularJS] Best Practise - Module
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 controller, factory, directive, service 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的更多相关文章
- 33.AngularJS 应用 angular.module定义应用 angular.controller控制应用
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 模块(Module) 定义了 AngularJS 应用. AngularJS 控制器(Co ...
- 淡淡理解下AngularJS中的module
在AngularJS中module是一个核心的存在,包括了很多方面,比如controller, config, service, factory, directive, constant, 等等. 在 ...
- [AngularJS] Best Practise - Minification and annotation
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...
- [AngularJS] Best Practise - Controller
ControllerAs: Use thecontrollerAs syntax always as it aids in nested scoping and controller instance ...
- AngularJS - 插件,module注入
Index.html <body> <div ng-app="myApp"> <div ng-controller="firstContro ...
- AngularJS - contorller app module
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- [AngularJS] Best Practise - Resolve promises in router, defer controllers
See more:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/ /** * Created by Answer12 ...
- AngularJs学习
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 带你走近AngularJS - 基本功能介绍
带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...
随机推荐
- gcc都做了什么优化
直接上程序: setjmp和longjmp是处理函数嵌套调用的,goto语句不能跨越函数,所以不选择goto. #include <setjmp.h> int setjmp(jmp_buf ...
- [质疑]编程之美求N!的二进制最低位1的位置的问题
引子:编程之美给出了求N!的二进制最低位1的位置的二种思路,但是呢?但是呢?不信你仔细听我道来. 1.编程之美一书给出的解决思路 问题的目标是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二 ...
- 使用curl操作openstack swift
openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/con ...
- C++ 串
♣ string 的基类basic_string中没有虚函数,它无意成为基类.更像是为了处理字符相关的问题而专门提供的一个工具及操作方法.如:想要在一个字符串str1中查找str2,没必要每次都去写K ...
- Netmask v. Address Prefix Length
Netmask Address Prefix Length Hosts / Class C's / Class B's / Class A's (Class C) / / , / , / , / , ...
- (转)PHP开发框架浅析
开发框架的定义我没有找到很准确的描述,下面几句话基本概括了开发框架的的功能和用途 框架是一种应用程序的半成品: 框架就像是人的骨骼一样: 框架是一组可复用的组件: 框架是一个可复用的设计构件…… 简而 ...
- 前N个自然数的随机置换
来自:[数据结构与算法分析——C语言描述]练习2.7 问题描述:假设需要生成前N个自然数的一个随机置换.例如,{4,1,2,5,2}和{3,1,4,2,5}就是合法的置换,但{5,4,1,2,1}却不 ...
- html5基础知识
html5+css3 html5定义很多简便东西和宽松语法: 文档头: <!doctype html> 文档编码: <meta cha ...
- JNLP + Applet + Bouncy Castle
http://stackoverflow.com/questions/4275005/jnlp-applet-bouncy-castle ——————————————————————————————— ...
- SurfaceView 和 TextureView
1.区别 The followings are two limitations of SurfaceView: You can not be animated, transformed and sca ...