[AngularJS + cryptoJS + Gravatar] Provider vs factory
Configurable Bits Need a Provider
We want to be able to configure the characterLength
before Tweetable
runs. Refactor the Tweetable
factory into a provider and expose asetLength()
function that will allow us to set a characterLength
in our app config.
angular.module('NoteWrangler')
.factory('Tweetable', ['$http', function TweetableFactory($http) {
var characterLength = 144; return function(potentialTweet) {
return $http({
method: 'POST',
url: 'http://gentle-spire-1153.herokuapp.com/tweet',
data: {
description: potentialTweet,
maxLength: characterLength
}
});
};
}]);
Change the factory definition into a provider definition.
.provider('Tweetable', ['$http', function TweetableProvider($http) {
Wrap the existing function returned by our TweetableProvider()
function in a call to the $get()
function required by providers. Don't forget to move the $http
service injection!
angular.module('NoteWrangler')
.provider('Tweetable', [function TweetableProvider() {
var characterLength = 144;
this.$get = function($http){
return function(potentialTweet) {
return $http({
method: 'POST',
url: 'http://gentle-spire-1153.herokuapp.com/tweet',
data: {
description: potentialTweet,
maxLength: characterLength
}
});
};
};
}]);
Create a setLength()
function attached to the provider that sets thecharacterLength
variable.
angular.module('NoteWrangler')
.provider('Tweetable', [function TweetableProvider() {
var characterLength = 144;
this.$get = function($http){
return function(potentialTweet) {
return $http({
method: 'POST',
url: 'http://gentle-spire-1153.herokuapp.com/tweet',
data: {
description: potentialTweet,
maxLength: characterLength
}
});
};
}; this.setLength = function(length){
characterLength = length;
};
}]);
Configuring the Tweet Length
Now that our provider is ready to go, let's call the setLength()
method ofTweetableProvider
to configure the acceptable maximum tweet length. Instead of 144 characters, we need to allow for a characterLength
of 40.
Let's call config()
on our NoteWrangler
module and provide it an anonymous function.
Inject TweetableProvider
into the config()
function.
Call the setLength()
function of TweetableProvider
from within the config()
function and pass it a value of 40.
angular.module('NoteWrangler', ['ngRoute'])
.config(function(TweetableProvider){
TweetableProvider.setLength(40);
});
Link: https://code.google.com/p/crypto-js/
[AngularJS + cryptoJS + Gravatar] Provider vs factory的更多相关文章
- angularjs中provider,factory,service的区别和用法
angularjs中provider,factory,service的区别和用法 都能提供service,但是又有差别 service 第一次被注入时实例化,只实例化一次,整个应用的生命周期中是个单例 ...
- AngularJS中的Provider们:Service和Factory等的区别
引言 看了很多文章可能还是不太说得出AngularJS中的几个创建供应商(provider)的方法(factory(),service(),provider())到底有啥区别,啥时候该用啥,之前一直傻 ...
- AngularJS深入(5)——provider
太精彩,不得不全文引用. 到这个层次,可能才敢说自己懂了吧... http://syaning.com/2015/07/21/dive-into-angular-5/ 在使用AngularJS的时候, ...
- 打造属于你的提供者(Provider = Strategy + Factory Method) 设计模式 - Provider Pattern(提供者模式)
打造属于你的提供者(Provider = Strategy + Factory Method) 1.1.1 摘要 在日常系统设计中,我们也许听说过提供者模式,甚至几乎每天都在使用它,在.NET F ...
- 【5min+】 设计模式的迷惑?Provider vs Factory
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- [译]AngularJS中几种Providers(Factory, Service, Provider)的区别
原文: http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/ 什么是Provider? Angula ...
- 深究AngularJS——自定义服务详解(factory、service、provider)
前言 3种创建自定义服务的方式. Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...
- AngularJS服务中serivce,factory,provider的区别
Angular服务是一个由服务工厂创建的单例对象.这些服务工厂是由 service provider 依次创建的.而service providers是构造函数.它们必须包含一个$get属性用于在实例 ...
- angularjs model.service vs provider vs factory?
<!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...
随机推荐
- Linux SocketCan client server demo hacking
/*********************************************************************** * Linux SocketCan client se ...
- 随心所欲的DateTime显示格式
任何项目,难免会碰到DateTime的显示问题,.net框架虽提供丰富多样的显示方法,但我很少使用,因老忘记细节,每次都要纠结到底月份在前还是年份在前:日期分隔符到底是“/”,还是“\”,还是“-”等 ...
- Android中Toast的用法简介
转自:http://www.cnblogs.com/GnagWang/archive/2010/11/26/1888762.html Toast是Android中用来显示显示信息的一种机制,和Dial ...
- mycat分布式mysql中间件(数据库切分概述)[转]
mysql数据库切分 前言 通 过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制,一旦数据库过于庞大,尤其是当写入过于频繁,很难由一台主机支撑的时 候,我们还是会面临到扩展 ...
- Log Explorer使用说明
一.介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复.你可以浏览日志.导出数据.恢复被修改或者删除的数据(包括执行过update,delete,drop和trunca ...
- uva 11019 Matrix Matcher
题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次. 思路:要想整个矩阵匹配,至少各行都得匹配.所以先把P的每行看做一个模式串构造出AC自动机,然后在T中的各行 ...
- uvalive 4795 Paperweight
题意:给出一个5个顶点的多面体以及多面体内一点P.求让 多面体不同的方式(即以不同的面)放在地面上,设这个着地的面为A,多面体重心在A上的投影为B,在保证B在A内部且距离A的各个边界不小于0.2的前提 ...
- Extjs4 页数重置
重新加载数据store.loadPage(1);就是到第一页了
- MyEclipse10 Tomcat7 JDK1.7 配置
第一步.MyEclipse10 Tomcat7 JDK1.7下载 MyEclipse10http://downloads.myeclipseide.com/downloads/products/ewo ...
- Hello,Ubuntu(安装过程中遇到的问题及解决)
2013-02-23 不折腾不舒服(>_<).在虚拟机上运行Ubuntu程序一多就明显卡顿,感觉效率不高.为了流畅使用Ubuntu,也便于将来学习Vim/Emacs,我决定在笔记本的Win ...