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的更多相关文章

  1. [AngularJS] Services, Factories, and Providers -- value & Providers

    Creating a Value Object Sometimes you have javascript object defined: //value object var droidValue ...

  2. 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?

    在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...

  3. AngularJs:Service、Factory、Provider依赖注入使用与区别

           本教程使用AngularJS版本:1.5.3        AngularJs GitHub: https://github.com/angular/angular.js/       ...

  4. 跟我学AngularJs:Service、Factory、Provider依赖注入使用与差别

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka        本教程使用AngularJs版本号:1.5.3        AngularJ ...

  5. [译]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 ...

  6. AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)

    目录[-] 一.service引导 二.service 1.factory() ‍2.service()‍ ‍3.provider()‍‍ 一.service引导 刚开始学习Angular的时候,经常 ...

  7. AngularJS中service,factory,provider的区别

    一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is whe ...

  8. AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)

    1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...

  9. Service vs Factory vs provider的迷惑

    刚开始我很迷惑的,但是经过一段时间的项目,还有看大漠老师的东西,似乎明白了,他们的区别也就是  一个人喜欢吃面还是吃饭或者肯德基区别.目的就是填饱肚子! 以下是它们在AngularJS源代码中的定义: ...

随机推荐

  1. iOS扫描二维码(系统方法)

    步骤如下: 1.导入AVFoundation框架,引入<AVFoundation/AVFoundation.h> 2.设置一个用于显示扫描的view 3.实例化AVCaptureSessi ...

  2. 关于typedef int(*lpAddFun)(int, int)

    lpAddFun是typedef定义的一个名称 可以用来定义变量 比如 lpAddFun p; 那 p就是 int(*p)(int, int); 首先(*p)说明p是一个指针,(*p)();说明p指向 ...

  3. python自学笔记

    python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...

  4. [转]Java远程方法调用

    Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...

  5. hdu 2460 poj 3694 (双联通+LCA)

    在给出的两个点上加一条边,求剩下桥的数量,,不会LCA在线,就用了最普通的,先Tarjan双联通缩点,然后将缩完的图建成一棵树,树的所有边就是桥了,如果在任意两点间加一条边的话,那么从两点到最近公共祖 ...

  6. [ JS 进阶 ] 如何改进代码性能 (3)

    原文链接 总结一下 1.减少操作dom的次数 2.需要多次使用某全局变量的时候,将其赋给一个局部变量,避免重复查找 3.优化循环 4.多用逗号和直接赋值的方式来var,减少工厂方式和构造函数方式创建对 ...

  7. ASP.NET程序从IIS6移植到IIS7时出现500.22错误

    最可能的原因:  •    此应用程序在 system.web/httpModules 节中定义配置.  可尝试的操作:  •    将配置迁移到 system.webServer/modules 节 ...

  8. iTunes 安装终极解决方案

    近日手贱升级了Itunes,升级过程即报失败,然后卸载所有相关东西,再重装,Itunes安装成功,但是报告无法使用iphone,经过几天摸索,发现是Apple Mobile Device Suppor ...

  9. Android 数据库读取数据显示优化 Application [6]

    Application和Activity,Service一样是android框架的一个系统组件, 当android程序启动时系统会创建一个application对象,用来存储系统的一些信息. 通常我们 ...

  10. python——BS解析器