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. CXF自动生成客户端

    官网下载apache-cxf-3.1.6,bin目录下.配置环境变量,生成客户端 http://blog.csdn.net/jaune161/article/details/25499939 http ...

  2. JavaScript typeof, null, 和 undefined

    typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型. 实例 typeof "John"                // 返回 string typeof ...

  3. Pythonchallenge一起来闯关(二)

    前情提要:Pythonchallenge一起来闯关(一) 这一篇来闯关10-15.感觉这几关比先前的难了不少,有的题目完全没思路. 10. 页面源码中的链接点击后有a = [1, 11, 21, 12 ...

  4. jquery 文本框聚焦文字删除

    做作业需要,自己写了一个,写的很烂. $(function() { $("#search_input").addClass("before_focus");/* ...

  5. IIS 7.5 部署ASP.Net MVC 网站

    請務必註冊 ASP.NET 4.0:若是 32 位元則是 %WINDIR%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -ir 1.首先确定已经安 ...

  6. Subversion 1.7 Eclipse integration in Ubuntu12(转载)

    原文链接:http://steveliles.github.io/subversion_1_7_eclipse_integration_in_ubuntu.html Getting Subversio ...

  7. Serilog with Autofac

    http://serilog.net/  ---不错的日志工具 1.直接绑定 builder.Register<ILogger>((c, p) => { return new Log ...

  8. c# 重新认识 Double 浮点型

    double test1 = 0; for (int i = 0; i < 100000000; i++) { test1 += 0.0001; } 请问 test1 的值是几? 答案是:999 ...

  9. 转:PHP 使用ZipArchive压缩文件并下载

    原文来自于:http://courages.us/archives/176 网站上需要提供一些打印数据给用户下载,这些文件每次都需要重新生成,因为随时都会有新的数据产生.网络上关于PHP的压缩功能实现 ...

  10. 转:嵌入式linux启动时运行的inittab文件

    嵌入式系统下的linux启动配置文件,不同与普通的PC linux启动配置,启动相关文件与文件的内容也要少得多.嵌入式系统下的linux启动过程一般是:     1 在bootloader中制定各种要 ...