[AngularJS + cryptoJS + Gravatar] Provider vs factory
Configurable Bits Need a Provider
We want to be able to configure the characterLength before Tweetableruns. 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 ...
随机推荐
- bzoj1426
偷个懒,转自hzwer [“这种煞笔题怎么总有人问”,被吧主D了... 用f[i]表示已经拥有了i张邮票,则期望还需要购买的邮票数 则f[n]=0 f[i]=f[i]*(i/n)+f[i+1]*((n ...
- jquery提示气泡
<link href="css/manhua_hoverTips.css" type="text/css" rel="stylesheet&qu ...
- poj2752 水题
又2b了一次…… var s:ansistring; ans,pre:..] of longint; i,k,tot:longint; procedure main; begin pre[]:=;k: ...
- NOI2002银河英雄传说
原先就看过这道题,觉得很复杂. 不知道为什么今天一看觉得好水啊…… 难道这就是并查集的启发式合并? 数组d[i]表示i到其父节点的距离,即中间隔了多少船舰. 数组sum[i]记录以i为根的集合总共有多 ...
- keybd_event跟SendMessage,PostMessage模拟键盘消息的区别 z
首先你会发现keybd_event函数中是没有窗口句柄作为参数的,好奇的你一定会觉得很奇怪,那是因为,keybd_event是全局模拟按键的,只对前台窗口(即当前的活动窗口)才可以,但是如果模拟的按键 ...
- error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7
工程使用了DirectDraw,编译出错 error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7 解决办法是吧dxguid.lib添加到工程中,把lib所在目录添加到工程 ...
- POJ3087 Shuffle'm Up 简单模拟
题意:就是给你两副扑克,然后一张盖一张洗牌,不断重复这个过程,看能不能达到目标的扑克顺序 分析:然后就模拟下,-1的情况就是有循环节 #include<cstdio> #include&l ...
- PowerShell:Linux程序员喜欢的cmd增强版
Linux程序员有时偶尔使用Windows下的cmd工具,会被逼疯的,有些命令ls, cat, ps等已经条件反射一样使用. 但在cmd下,根本不知道该用什么命令,好在盖兹大叔照顾了此部分需求.从Vi ...
- 转:maven项目添加jar包.
很多新手都不知道如何在maven项目里添加jar包. 以前我还没接触maven的时候下载过一个demo,是maven项目. 我居然是照着他的pom.xml文件一个一个的写!!! 很多人认为理所当然的东 ...
- Zabbix探索:Zabbix API使用时的错误1
好久没来了,最近比较疲倦,估计是从年初到现在没有休息的原因. 刚刚批量导入网络设备到Zabbix时,报错: Cannot implode expression 后面自然是IP和触发器的表达式. 最初还 ...