我也谈“the difference between Factory, Service, and Provider in Angular”
看完这篇文章之后的理解与实践:原文地址:http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/
<!doctype html>
<html ng-app="myModule">
<head>
<script src="../lib/js/angular.min.js"></script>
</head>
<body>
<script>
var myModule = angular.module('myModule', []);
myModule.factory('greeter', function($window){
return {
greet: function(text){
$window.alert(text);
}
};
}); function myController($scope,greeter,notify){
$scope.sayHello = function(){
greeter.greet('Hello, world!');
}
$scope.callNotify = function(msg){
notify.say(msg);
};
// scope.callNotify = function(msg){
// notify(msg);
// };
} // myModule.factory('notify', function($window){
// var msgs = [];
// return function(msg){
// msgs.push(msg);
// if(msgs.length==3){
// console.info(msgs);
// $window.alert(msgs.join("\n"));
// msgs = [];
// }
// }
// });
myModule.service('notify', function($window){
var msgs = [];
return this.say = function(msg){
msgs.push(msg);
if(msgs.length==3){
console.info(msgs);
$window.alert(msgs.join("\n"));
msgs = [];
}
}
});
</script>
<div ng-controller="myController">
<button ng-click="sayHello()">Hello</button>
<input type="text" ng-model="message" />
<button ng-click ="callNotify({{'message'}})">Notify</button>
</div>
</body>
</html>
我也谈“the difference between Factory, Service, and Provider in Angular”的更多相关文章
- angularjs中factory, service和provider
在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了(因为service的底 ...
- AngularJS 之 Factory vs Service vs Provider【转】
英文原文:AngularJS: Factory vs Service vs Provider 当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一 ...
- AngularJS之Factory vs Service vs Provider
原文 http://www.linuxeden.com/html/news/20140509/151538.html 当你初试 Angular 时,很自然地就会往 controller 和 scop ...
- AngularJS 讲解五, Factory ,Service , Provider
一. 首先说一下,为什么要引入Factory,Service和Provider这三个Service层. 1.因为我们不应该在controller层写入大量的业务逻辑和持久化数据,controller层 ...
- AngularJS 中的 factory、 service 和 provider区别,简单易懂
转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...
- [转载]AngularJS之Factory vs Service vs Provider
http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider http://tylermcginnis.com/a ...
- AngularJS 笔记之创建服务方式比较 : factory vs service vs provider 。
首先说一下服务这个东西是用来干嘛的.很多时候我们把太多的数据和逻辑都一股脑儿地往 controller 里放.这样我们的 controller 原来越臃肿.从它们的生命周期可以发现,其实 contro ...
- Angular之Providers (Value, Factory, Service and Constant )
官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...
- Angular1.x 之Providers (Value, Factory, Service and Constant )
官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...
随机推荐
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- fragement切换动画效果的实现
标准动画: fragementTransaction.setTransition(FragmentTransation.TRANSIT_FRAGMENT_CLOSE); 自定义动画: fragemen ...
- POJ 1966 ZOJ 2182 Cable TV Network
无向图顶点连通度的求解,即最少删除多少个点使无向图不连通. 我校“荣誉”出品的<图论算法理论.实现及其应用>这本书上写的有错误,请不要看了,正确的是这样的: 对于每个顶点,分成两个点,v和 ...
- js通过keyCode值判断单击键盘上某个键,然后触发指定的事件
当单击按键时触发事件 document.onkeydown = function (e) { e = e || event; if (e.keyC ...
- Hibernate与Sleep的区别
转自:http://blog.sina.com.cn/s/blog_4b6e98810100n37k.html 休眠(Hibernate),将系统切换到该模式后,系统会自动将内存中的数据全部转存到硬盘 ...
- 解读QML之一
http://cache.baiducontent.com/c?m=9d78d513d98002b8599dcb201a17a7374408c6347691c4523f8a9c12d522195646 ...
- Java学习笔记之自定义异常
1.自定义异常类: /** * 自定义异常,只要继承继承Exception类或其子类即可 * @author Administrator * */ public class FileException ...
- ORA-12170: TNS:Connect timeout occurred
VM 作为ORACLE 服务器,客户端登陆提示超时,本地连接使用网络连接正常. D:>sqlplus system/oracle123@//192.168.63.121:15021/pdb01 ...
- spring多个数据源配置
sys.properties中的内容 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver DB.url=jdbc\:oracle\:thin\:@ ...
- Provisioning profile 浅析
转载自: http://blog.csdn.net/chenyufeng1991/article/details/48976245 一般在我们代码编写中不会用到Provisioning prof ...