我也谈“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 ...
随机推荐
- How to set a MySQL root user password in MAC OSX
https://www.youtube.com/watch?v=sFT9tGL54sI
- VNC VIEWER的使用集锦
关于颜色深度的问题, 今天用VNC Viewser ,连上去之后,发现色彩可能只有8或者16位 然后修改了 sever的depth,也不好使, 于是,就修改了 client的 colourlevel ...
- 在Activity之间传递数据—传递值对象
传递有两种方式,一种是类继承自Serializable(Java方式,速度较慢),另一种是类继承自Parcelable(Android方式) 继承自Serializable的时候,实现比较简单,类只需 ...
- 谈CSS模块化【封装-继承-多态】
第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...
- MySQL数据库分区修改【原创】
之前有个表分区添加时s201607添加成s201617,所以在查询7月份数据时报错 错误的 alter table statistics_ticket add partition (partition ...
- vs 发布web应用程序时,找不到cs文件错误
将*.aspx.*.ascx.*.master所有出错页面文件中的 CodeFile="******.aspx.cs" 批量替换成 Codebehind="******. ...
- wifi的UI控制。打不开UI
部分主要是您要清楚WiFi作为什么功能,即WiFi是仅仅作为station功能,还是要开启p2p功能,当WiFi作为station功能时候开启的是wpa_supplicant服务,当WiFi作为p2p ...
- spring xsd
http://ljhzzyx.blog.163.com/blog/static/3838031220132239471608/ spring配置文件报找不到xsd文件错误 2013-03-23 10: ...
- python 多线程 paramiko实现批量命令输入输出
远程批量执行命令 实现多线程执行 速度快 实现多并发登录 #-*- coding: utf-8 -*- #!/usr/bin/python import paramiko import threadi ...
- cordova sqlite
jar包在这里下载 https://github.com/litehelpers/Cordova-sqlite-storage 把SQLitePlugin 复制到自己工程目录 org.pgsqlite ...