[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源代码中的定义: ...
随机推荐
- Python网页爬虫(一)
很多时候我们想要获得网站的数据,但是网站并没有提供相应的API调用,这时候应该怎么办呢?还有的时候我们需要模拟人的一些行为,例如点击网页上的按钮等,又有什么好的解决方法吗?这些正是python和网页爬 ...
- spring 入门笔记(一)
最近学习spring 通过笔记形式加深自己对spring的理解,也希望能跟各位入门者分享和讨论. 一.下载spring 下载spring也费了不少功夫,目前还没从spring官网找到下载入口,我从下面 ...
- 一元云购完整源码 云购CMS系统 带安卓和ios手机客户端
看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己反编译. 这里不做功能和其它更多的介绍,可以自己下载后慢慢测试了解. 下面演示图为亲测截图< ...
- Linux 下 安装 Phalcon
先安装GIT 然后从 git://github.com/phalcon/cphalcon.git 这里下载安装文件 编译完成就可以安装了! 编译chmod -R 777 cphalcon1. 创建从C ...
- Python报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法
SyntaxError: Non-ASCII character '\xe5' in file 原因:Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他的 ...
- 07:打印ASCII码
总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个除空格以外的可见字符(保证在函数scanf中可使用格式说明符%c读入),输出其ASCII码. 输入 一个除空格以外的可见字符. ...
- 周赛F题 POJ 1458(最长公共子序列)
F - F Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description ...
- Android模拟器Genymotion安装向导
Genymotion简述 Genymotion提供Android虚拟环境的工具集.相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了.如果你没有物理机器,又不想忍受官方模拟 ...
- IIS 10 安装URLRewrite组件 方式
1.Open Regedit > HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp 2.Edit "MajorVersion" an ...
- 习题二:string数组应用
说明: 读字符串char buf[100]="xxx:yyy:zzz:aaa:bbb" 按“:”进行分解到string数组中去 逻辑: 通过指针遍历整个字符串 遇到'\0'表示字符 ...