module

目前我选编写的都是在AngularJs-1.5版本,如有疑问可以联系我。

理解模块的生命周期。

config 和 run 方法是模块调用时加载的方法。那么module的执行顺序是怎么样呢。

config方法是在module 被加载后调用的方法。run 方法是在所有的模块都被加载后调用的方法。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="exampleApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Cycle</title>
<script src="angular.js"></script>
<link href="bootstrap-theme.css" rel="stylesheet" />
<link href="bootstrap.css" rel="stylesheet" />
<script>
var app = angular.module("exampleApp", ["exampleApp.Services"]);
app.constant("startTime", new Date().toLocaleDateString());
app.config(function (startTime) {
console.log("Main Module config:" + startTime);
});
app.run(function (startTime) {
console.log("Main module run:" + startTime);
});
var now = new Date();
app.value("nowValue", now);
angular.module("exampleApp.Services", [])
.service("days", function (nowValue) {
this.today = nowValue.getDay();
this.tomorrow = this.today + 1;
})
.config(function () {
console.log("Services module config:" + "(no time)");
})
.run(function (startTime) {
console.log("Services module run:" + startTime);
});
</script>
</head>
<body > </body>
</html>

  运行结果:

    

创建

 var app = angular.module("exampleApp", ["exampleApp.Services"]);
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="exampleApp">

    module 函数包含三个参数(name,requires,config);

name:表示需要创建模块的名字如:exampleApp;

requires:表示创建时所依赖的模块的名字。

config:表示注册模块回调的函数。//相当于 app.config();

 var app = angular.module("exampleApp", ["exampleApp.Services"], function (startTime) {
console.log("Main Module config:" + startTime);
});

引用

var app = angular.module("exampleApp");

  上面的表示,exampleApp 的module已被创建,当出现这句代码,AngularJs就会查找该模块。

summary

在使用模块时,AngularJs保证了主模块所依赖的模块的回调函数先解析。由于run方法是在所有的模块都加载后才执行的。所有,当依赖模块的config解析之后,等到所有的模块都被加载,在执行依赖的run方法,最后是主模块的run方法。

最新文章

  1. Anaconda 用于科学计算的 Python 发行版
  2. 数据仓库之启用cdc
  3. BZOJ2186 欧拉函数
  4. 【视频】从零开始编写第一个PHP扩展
  5. 整理:sql语句优化之SQL Server
  6. void (*f(int, void (*)(int)))(int) 函数解析 转
  7. SSD -----TLC MLC SLC
  8. pstree命令
  9. NET站点Web部署
  10. 迁移笔记:php缓存技术memcached
  11. JS代码混淆 支持PHP .NET PERL
  12. O the joy of having nothing / 아무것도 갖지않고
  13. 随机生成器、thread(暂停)、清屏定义
  14. Oracle执行计划顺序
  15. inline-block解决
  16. c time类型详解
  17. Ext.js入门(二)
  18. centos7 下 apache nginx squid https正向代理 代理服务器
  19. es6初级之解构----之二 及 键值反转实现
  20. 微信redirect_uri 回调错误,scope权限错误

热门文章

  1. [Leetcode][Python]43: Multiply Strings
  2. poj1163 dp入门
  3. H面试程序(11): 判断字符串是否包含子串问题
  4. 使用Web Application Stress Tool 进行压力测试
  5. 使用sumlime text有感
  6. 阻止JS事件冒泡传递(cancelBubble 、stopPropagation)
  7. android的color整理(一)
  8. 华为S5700基础配置----备份和恢复配置文件
  9. 疯狂Java讲义 第一章控制台五子棋(代码分析)
  10. js选项卡切换效果