angularjs学习笔记-封装公共模块
2016-10-19 20:12
309 查看
1、$rootscope和$scope的区别和作用
$scope是angularJS中的作用域(其实就是存储数据的地方),很类似javascript的原型链 。搜索的时候,优先找自己的scope,如果没有找到就沿着作用域链向上搜索,直至到达根作用域rootScope。
$rootScope是由angularJS加载模块的时候自动创建的,每个模块只会有1个rootScope。rootScope创建好会以服务的形式加入到 $injector中。也就是说通过 $injector.get("$ rootScope ");能够获取到某个模块的根作用域。更准确的来说,$rootScope是由angularJS的核心模块ng创建的。
scope是html和单个controller之间的桥梁,数据绑定就靠他了。rootscope是各个controller中scope的桥梁。用rootscope定义的值,可以在各个controller中使用。
2、angularjs封装公共服务:http://docs.angularjs.cn/guide/concepts。
angular.module('invoice2', ['finance2'])
.controller('InvoiceController', ['currencyConverter', function(currencyConverter) {
this.qty = 1;
this.cost = 2;
this.inCurr = 'EUR';
this.currencies = currencyConverter.currencies;
this.total = function total(outCurr) {
return currencyConverter.convert(this.qty * this.cost, this.inCurr, outCurr);
};
this.pay = function pay() {
window.alert("Thanks!");
};
}]);
$scope是angularJS中的作用域(其实就是存储数据的地方),很类似javascript的原型链 。搜索的时候,优先找自己的scope,如果没有找到就沿着作用域链向上搜索,直至到达根作用域rootScope。
$rootScope是由angularJS加载模块的时候自动创建的,每个模块只会有1个rootScope。rootScope创建好会以服务的形式加入到 $injector中。也就是说通过 $injector.get("$ rootScope ");能够获取到某个模块的根作用域。更准确的来说,$rootScope是由angularJS的核心模块ng创建的。
scope是html和单个controller之间的桥梁,数据绑定就靠他了。rootscope是各个controller中scope的桥梁。用rootscope定义的值,可以在各个controller中使用。
2、angularjs封装公共服务:http://docs.angularjs.cn/guide/concepts。
angular.module('invoice2', ['finance2'])
.controller('InvoiceController', ['currencyConverter', function(currencyConverter) {
this.qty = 1;
this.cost = 2;
this.inCurr = 'EUR';
this.currencies = currencyConverter.currencies;
this.total = function total(outCurr) {
return currencyConverter.convert(this.qty * this.cost, this.inCurr, outCurr);
};
this.pay = function pay() {
window.alert("Thanks!");
};
}]);
相关文章推荐
- angularjs学习笔记之三大模块(modal,controller,view)
- jq框架封装学习笔记5-样式操作与属性操作模块
- 【AngularJS学习笔记】封装一些简单的控件(封装成Html标签)
- jq框架封装学习笔记2-选择模块
- Python学习二:公共日志记录模块封装
- jq框架封装学习笔记4-DOM操作模块
- [Firefly引擎][学习笔记三][已完结]所需模块封装
- react native学习笔记26——iOS原生模块的封装与调用
- react native学习笔记25——Android原生模块的封装与调用
- angularjs学习笔记之三大模块(modal,controller,view)
- AngularJs学习笔记(5)--模块(module)
- AngularJs学习笔记(5)--模块(module)
- 传智播客JDBC_规范和封装jdbc程序代码学习笔记
- Javascript学习笔记9——prototype封装继承
- 微软企业库4.1学习笔记(四)主要的功能模块简介
- Python学习笔记---模块;文件与文本处理
- Drupal学习笔记-1.模块入门
- Ruby学习笔记四——模块
- 微软企业库4.1学习笔记(十四)缓存模块2 使用缓存模块进行开发
- Enterprice library学习笔记之一:【初探】缓存应用模块