解析AngularJS service vs factory

原文来自:http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/

What is an AngularJS service or factory?

Singleton.

Yes! That one word is enough to define AngularJS services. The purpose of AngularJS service / factory function is to generate a single object or function that represents the service to rest of the application. That object or function is passed as a parameter to any other factory function which specifies a dependency on this service.

Services

Syntaxmodule.service(‘serviceName‘, function);

Result: When declaring serviceName as an injectable argument you will be provided with the instance of a function passed to module.service.

Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call(this) or similar.

Example:

 1 varapp = angular.module(‘myApp‘, []);
 2
 3 // Service definition
 4 app.service(‘testService‘, function(){
 5     this.sayHello= function(text){
 6         return"Service says \"Hello "+ text + "\"";
 7     };
 8 });
 9
10 // AngularJS Controller that uses the service
11 functionHelloCtrl($scope, testService)
12 {
13     $scope.fromService = testService.sayHello("World");
14 }

Factories

Syntaxmodule.factory(‘factoryName‘, function);

Result: When declaring factoryName as an injectable argument you will be provided the value that is returned by invoking the function reference passed to module.factory.

Usage: Could be useful for returning a ‘class’ function that can then be new’ed to create instances.

 1 varapp = angular.module(‘myApp‘, []);
 2
 3 // Factory
 4 app.factory(‘testFactory‘, function(){
 5     return{
 6         sayHello: function(text){
 7             return"Factory says \"Hello "+ text + "\"";
 8         }
 9     }
10 });
11
12 // AngularJS Controller that uses the factory
13 functionHelloCtrl($scope, testFactory)
14 {
15     $scope.fromFactory = testFactory.sayHello("World");
16 }
时间: 2024-09-29 23:29:43

解析AngularJS service vs factory的相关文章

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

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka        本教程使用AngularJs版本:1.5.3        AngularJs GitHub: https://github.com/angular/angular.js/        AngularJs下载地址:https://angularjs.org/ 用有过Spring的人都知道,Spring的核心思想就是DI(依赖注入,Dependency Injection)和I

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和Factory有什么不同,我应该使用哪个? 这篇文章将讲解service和factory的不同之处,为什么我们喜欢service多过于factory. Service和Factory的不同之处 在AngularJS中service和factory有什么不同? 我们可以这样定义一个service: app

AngularJS Service vs Factory 总结(持续更新中...)

首先,看一看方法: factory: factory(name, $getFn); service: service(name, constructor); 再看看例子使用: 定义一个angular module var module = angular.module('myapp', []); 使用service: module.service('userService', function(){     this.users = ['John', 'James', 'Jake']; });

Angularjs service vs factory vs provider

Angular provides us with three ways to create and register our own service. 1) Factory 2) Service 3) Provider TL;DR 1) When you’re using a Factory you create an object, add properties to it, then return that same object. When you pass this service in

AngularJS进阶(三十三)书海拾贝之简介AngularJS中使用factory和service的方法

简介AngularJS中使用factory和service的方法 AngularJS支持使用服务的体系结构"关注点分离"的概念.服务是JavaScript函数,并负责只做一个特定的任务.这也使得他们成为维护和测试的单独实体.控制器,过滤器可以调用它们作为需求的基础.服务使用AngularJS的依赖注入机制注入正常. AngularJS提供例如许多内在的服务,如:$http, $route, $window, $location等.每个服务负责例如一个特定的任务,$http是用来创建AJ

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

目录[-] 一.service引导 二.service 1.factory() ‍2.service()‍ ‍3.provider()‍‍ 一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is where we'll start the twenty-five days of Angular calendar. 二.service 在Angular里面,se

angular之service、factory预provider区别

昨晚项目组做了angular分享,刚好有讨论到这个问题.虽然许久不做前端开发,但是兴趣所致.就查阅了下资料,以便后续需要使用 自己的理解:service是new出来的,factory是直接使用就能获得到service对象,service多了一个this.provider可以初始化注入之前进行一些全局配置,还有就是需要通过$get方法来获得 比较简单的一个理解 app.factory('a', fn); app.service('b', fn); app.provider('c', fn); Th

Service vs Factory vs provider的迷惑

刚开始我很迷惑的,但是经过一段时间的项目,还有看大漠老师的东西,似乎明白了,他们的区别也就是  一个人喜欢吃面还是吃饭或者肯德基区别.目的就是填饱肚子! 以下是它们在AngularJS源代码中的定义: 1 2 3 4 5 6 7 8 9 function factory(name, factoryFn) {     return provider(name, { $get: factoryFn }); } function service(name, constructor) {     ret

AndroidIntentService完全解析当Service遇到Handler(转载)

AndroidIntentService完全解析当Service遇到Handler 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚,在Android的开发中,凡是遇到耗时的操作尽可能的会交给Service去做,比如我们上传多张图,上传的过程用户可能将应用置于后台,然后干别的去了,我们的Activity就很可能会被杀死,所以可以考虑将上传操作交给Service