[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 ...
 
随机推荐
- 结构体buf_page_t
			
/** Buffer page (uncompressed or compressed) */ typedef struct buf_page_struct buf_page_t; struct bu ...
 - php 连接字符串. ZEND_ASSIGN_CONCAT/ZEND_CONCAT原理
			
0.php代码 <?php $a='abc'; $b='def'; $c='ghi';$d='jkl'; $a.=$b.$c.$d; 1.BNF范式(语法规则) expr_without_var ...
 - javascript在页面head内动态插入style
			
纯js实现: var css = 'h1 { background: red; }', head = document.getElementsByTagName('head')[0], style = ...
 - LBS云端数据删除和上传
			
这里采用C#模拟表单提交,实现LBS云端删除和csv格式文件的上传. 删除: /// <summary> /// 从LBS云端删除数据 /// </summary> /// & ...
 - UVa 1605 (构造) Building for UN
			
题意: 有n个国家,要设计一栋长方体的大楼,使得每个单位方格都属于其中一个国家,而且每个国家都要和其他国家相邻. 分析: 紫书上有一种很巧妙的构造方法: 一共有2层,每层n×n.一层是每行一个国家,另 ...
 - NOI2010能量采集(数论)
			
没想到NOI竟然还有这种数学题,看来要好好学数论了…… 网上的题解: 完整的结题报告: 首先我们需要知道一个知识,对于坐标系第一象限任意的整点(即横纵坐标均为整数的点)p(n,m),其与原点o(0,0 ...
 - Java [leetcode 33]Search in Rotated Sorted Array
			
题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
 - 在ubuntu14.04上编译android4.2.2系统
			
原创作品,转载请注明出处,严禁非法转载 copyright:weishusheng 2015.8.1 时下android系统非常流行,下面就来说一下,下载编译一个andro ...
 - C#生成缩略图代码
			
/**//// <summary> /// 生成缩略图 /// </summary> /// <param name=&q ...
 - 自己常用的js方法
			
$(function(){ tabview("#zcfw_list1"); tabview("#zcfw_list2"); tabview("#zcf ...