看完这篇文章之后的理解与实践:原文地址: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”的更多相关文章

  1. angularjs中factory, service和provider

    在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了(因为service的底 ...

  2. AngularJS 之 Factory vs Service vs Provider【转】

    英文原文:AngularJS: Factory vs Service vs Provider 当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一 ...

  3. AngularJS之Factory vs Service vs Provider

    原文  http://www.linuxeden.com/html/news/20140509/151538.html 当你初试 Angular 时,很自然地就会往 controller 和 scop ...

  4. AngularJS 讲解五, Factory ,Service , Provider

    一. 首先说一下,为什么要引入Factory,Service和Provider这三个Service层. 1.因为我们不应该在controller层写入大量的业务逻辑和持久化数据,controller层 ...

  5. AngularJS 中的 factory、 service 和 provider区别,简单易懂

    转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...

  6. [转载]AngularJS之Factory vs Service vs Provider

    http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider http://tylermcginnis.com/a ...

  7. AngularJS 笔记之创建服务方式比较 : factory vs service vs provider 。

    首先说一下服务这个东西是用来干嘛的.很多时候我们把太多的数据和逻辑都一股脑儿地往 controller 里放.这样我们的 controller 原来越臃肿.从它们的生命周期可以发现,其实 contro ...

  8. Angular之Providers (Value, Factory, Service and Constant )

    官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...

  9. Angular1.x 之Providers (Value, Factory, Service and Constant )

    官方文档Providers Each web application you build is composed of objects that collaborate to get stuff do ...

随机推荐

  1. iOS tableView的系统分割线定格设置以及分割线自定制

    一.关于分割线的位置. 分割线的位置就是指分割线相对于tableViewCell.如果我们要根据要求调节其位置,那么在iOS7.0版本以后,提供了一个方法如下: if ([self.tableView ...

  2. VMware+Windbg双机调试

    虚拟机使用XP系统:

  3. DB9_公头_母头_串口引脚定义及RS-232串口线制作方法

    RS-232连接线制作方法 材料及工具 一根双绞线(8芯).一个标准RJ45头.一个DB9孔型插头.一把RJ45专用工具.一个电烙铁及若干焊锡. 引脚定义 按以下管脚定义制作RJ45端头:I表示网络视 ...

  4. ACboy needs your help again!

    ACboy needs your help again! Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Ja ...

  5. 【Python爬虫实战--1】深入理解urllib;urllib2;requests

    摘自:http://1oscar.github.io/blog/2015/07/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3urllib;urllib2;reques ...

  6. 安卓获取线程id

    错误的做法: @Override public void onCreate() { mContext = getApplicationContext(); mHandler = new Handler ...

  7. LNMP环境的安装配置

    0.安装必要的依赖软件 如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作. yum -y install bzip2-devel curl-devel freetype-devel gcc ...

  8. Java多态(一)

    父类: public class Parent { public String name; private String pass; public void say1(AA aa){ System.o ...

  9. CocoaAsyncSocket框架的简单封装

    在iOS开发中使用socket(CFNetwork),一般都是用第三方库AsyncSocket. 参考博客:http://my.oschina.net/worldligang/blog/396881? ...

  10. 多说【最近访客】JS插件通用代码使用方法

    多说[最近访客]JS插件可以显示近期访问过你的博客并且已经使用向多说授权过的社交网络账号登录的用户头像,效果如下图所示. 最近访客JS插件的添加步骤如下: 1. 添加多说公用JS代码,每个页面只需添加 ...