[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 ...
随机推荐
- 对于requirejs AMD模块加载的理解
个人人为使用模块化加载的优点有三: 1,以我的项目为例:90%为图表展示,使用的是echarts,此文件较大,requirejs可以在同一个版本号(urlArgs)之下缓存文件,那么我就可以在访问登陆 ...
- sencha项目升级
对于已经开发好的sencha项目进行升级,要做的有以下几步(以sencha2.2.0升级到sencha2.3.1为例): 1,下载Sencha-2.3.1sdk,下载地址:http://cdn.sen ...
- NOI2013矩阵游戏
Description 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的 ...
- [.NET MVC进阶系列03] Views 视图基础
[注:此文对应Chapter 3:Views] 一.View的功能: 1.View用来呈现页面UI,通过Controller来指定View: 要注意的是,MVC和以前基于文件的Web应用不同,URL指 ...
- HDU 5335 Walk Out
题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...
- BinaryReader和BinaryWriter的leaveOpen参数 z
在.NET 4.5后,微软为BinaryWriter和BinaryReader类型的构造函数中加入了leaveOpen参数,当该参数为true后,BinaryReader或者BinaryWriter关 ...
- Live555研究之二Sleep实现
Live555通过一个while循环来不断读取socket,判断是否有连接进来,但是Live555并没有使用Sleep函数来让线程休眠多少毫秒来降低CPU占用率.Live555是通过select函数来 ...
- Can't find file: './mysql/plugin.frm' (errno: 13)[mysql数据目录迁移错位]错误解决
大概需要4个步骤,其中第1步通过service mysql stop停止数据库,第4步通过service mysql start启动数据库. 第2步移动数据文件,不知道是否为Ubuntu智能的原因,移 ...
- 6.2 CUDA streams
stream是什么 nivdia给出的解释是:A sequence of operations that execute in issue-order on the GPU. 可以理解成在GPU上执 ...
- JAVA面试题——JAVA基础篇
1.JAVA多态的实现方式:继承.重载.覆盖 2.JAVA有8种基本数据类型:byte.short.int.long.float.double.boolean.char 3.final.finally ...