[AngularJS] Services, Factories, and Providers -- Service vs Factory
Creating a Service:
Before actual create an angular service, first create a constructor in Javascript:
//constructor function
function DroidService() {
this.name = '';
} DroidService.prototype.speak = function () {
return "Hi I am " + this.name;
};
Then you we want to use this constutor function, you need to new an instance:
var droid = new DroidService();
droid.name = 'r2-d2';
console.log(droid.speak());
What need to understand here is that, you when do new opration, under the hook, Javascript does tow thing for you:
function DroidService() {
// var this = {}
this.name = '';
// return this;
}
First is var a new this object, then return this object.
Angular service is a constructor function.
//constructor function
function DroidService() {
this.name = '';
} DroidService.prototype.speak = function () {
return "Hi I am " + this.name;
}; angular.module('app', [])
.service('droid', DroidService)
.controller('DroidController', DroidController); function DroidController(droid) {
var droidCtrl = this;
droid.name = 'r2-d2';
droidCtrl.message = droid.speak(); }
When you create a service in angular, it helps to 'new' the constructor (service), then inject this instance whenever you want to use it.
Creating a Factory:
You can create an function which return an object:
function droidFactory() {
function speakingPrivately() {
return "Hi I am " + this.name;
} return {
name: '',
speak: speakingPrivately
};
}
This is called reaveling modular partten, because you can choose which function or props to be public or private by include those into return object.
Then you just need to invoke the function, you can get the object.
var droid = droidFactory();
droid.name = 'c3-po';
console.log(droid.speak());
Angular Factory is a function which return an object. (No constructor fucntion, no new opreation):
//revealing module pattern
function droidFactory() {
function speakingPrivately() {
return "Hi I am " + this.name;
} return {
name: '',
speak: speakingPrivately
};
} angular.module('app', [])
.factory('droid', droidFactory)
.controller('DroidController', DroidController); function DroidController(droid) {
var droidCtrl = this;
droid.name = 'c3-po';
droidCtrl.message = droid.speak();
}
When you create a factory, Angular will help to invoke the function so when you inject into controller, you will get the object back.
[AngularJS] Services, Factories, and Providers -- Service vs Factory的更多相关文章
- [AngularJS] Services, Factories, and Providers -- value & Providers
Creating a Value Object Sometimes you have javascript object defined: //value object var droidValue ...
- 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?
在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...
- AngularJs:Service、Factory、Provider依赖注入使用与区别
本教程使用AngularJS版本:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ ...
- 跟我学AngularJs:Service、Factory、Provider依赖注入使用与差别
林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 本教程使用AngularJs版本号:1.5.3 AngularJ ...
- [译]AngularJS Service vs Factory - Once and for all
原文: http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html Service和Fa ...
- AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)
目录[-] 一.service引导 二.service 1.factory() 2.service() 3.provider() 一.service引导 刚开始学习Angular的时候,经常 ...
- AngularJS中service,factory,provider的区别
一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is whe ...
- AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)
1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...
- Service vs Factory vs provider的迷惑
刚开始我很迷惑的,但是经过一段时间的项目,还有看大漠老师的东西,似乎明白了,他们的区别也就是 一个人喜欢吃面还是吃饭或者肯德基区别.目的就是填饱肚子! 以下是它们在AngularJS源代码中的定义: ...
随机推荐
- asp.net使用dorpdownlist绑定无限级分类
/// <summary> /// 绑定父项分类 /// </summary> protected void DDLBindClass( ...
- SQLServer2008收缩数据库日志
-- Set to SIMPLE mode ALTER DATABASE [DATABASE_NAME] SET RECOVERY SIMPLE; -- Shrink the db ); -- Set ...
- 2D简单图形相关算法罗列
因为平常在Qt开发过程中经常会与一些简单的2D几何图形打交道,因此学习和掌握一些基本的2D几何计算还是很有必要的,在这里罗列一些常用的基本情况,之后会适时补充. [1] 两点之间距离,根据两个点的差值 ...
- TCP/IP-入门
Life is not a rehearsal "人生没有彩排" 参考资料:TCP/IP入门经典 (第五版) TCP/IP详解 卷一:协议 一.什么是TCP/IP TCP/IP是一 ...
- (git fetch git push git pull)远程本地分支互相推送更新
git push origin bug_huiyuan:mobile_attribution 把bug_huiyuan(本地分支) 推送到 远程mobile_attribution分支 git pus ...
- php while循环 指定显示内容 例如不想显示前10条和后10条
<?php //查询信息总的条数 $db_num = query_num("表","where 1=1"); //每页显示的条数 $page_size=2 ...
- twisted(1)--何为异步
早就想写一篇文章,整体介绍python的2个异步库,twisted和tornado.我们在开发python的tcpserver时候,通常只会用3个库,twisted.tornado和gevent,其中 ...
- 关于自定义Adapter实现ListView的使用
以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件: 需要注意以下的几点: 1.自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注 ...
- uva 725 Division(除法)暴力法!
uva 725 Division(除法) A - 暴力求解 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- spring mvc标准项目结构
src com.xxx.inews.dao com.xxx.inews.dao.impl com.xxx.inews.data.entity com.xxx.inews.data.vo com.xxx ...