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. EBS与FMW集成工作流管理器的检查

    工作流管理器的检查点(DB层面): --1:数据库job aq参数设置,建议设置job_queue_processes>=10 select p.NAME,p.DESCRIPTION,p.VAL ...

  2. Semaphore (通常用于限制可以访问某些资源(物理或逻辑的)的线程数目)

    Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目.例如,下面的类使用信号量控制对内容池的访问: 方法详解: 构造方法摘要 Semaphore(int permits)     ...

  3. 66 Plus One(大数+1Easy)

    题目意思:vector<int> v存数   eg.123  则v[0]=1,v[1]=2,v[2]=3,加1后返回一个vector 思路:先判断新的vector长度是否需要加1,然后从v ...

  4. DropDownList自动生成年月日

    DropDownList自动生成年月日 aspx页面上有三个dropdownlist控件, dropdownlist1 表示年,dropdownlist2表示月,dropdownlist3表示天: 注 ...

  5. .net别样外观控件包DotNetBar

    内容介绍:http://www.componentcn.com/?thread-6423-1.html BubbleBar应用: BubbleBar, DevComponents.    Namesp ...

  6. groovy构建和解析xml文件

    原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/ 代码示例: 构建xml文件: def static createXmlFile(){ ...

  7. iOS开发网络篇—XML数据的解析

     iOS开发网络篇—XML数据的解析 iOS开发网络篇—XML介绍 一.XML简单介绍 XML:全称是Extensible Markup Language,译作“可扩展标记语言” 跟JSON一样,也是 ...

  8. 转载——web前端相关资源总结

    前端牛人博客:张克军.阮一峰.拔赤(李晶).拔赤(李晶)2.张鑫旭.梦想天空.阿当.泽飞.刘杰(嗷嗷).为之漫笔(李松峰).goddyzhao.hax的技术部落.周爱民.随网之舞.子鼠.司徒正美.ju ...

  9. spring restful 中文乱码问题

    进行如下配置: @RequestMapping( value="/zzs/xgm", produces="application/json;charset=utf-8&q ...

  10. GCC警告提示错误“cc1:all warnings being treated as errors”

    http://blog.csdn.net/zhangjs0322/article/details/25131787