[AngularJS + Cryptojs + Gravatar] Provider vs Factory

Source: Internet
Author: User

Configurable Bits need a Provider

We want to is able to configure the characterLength before Tweetable runs. Refactor the Tweetable factory into a provider and expose a function, that would allow setLength() us to set a with our characterLength app config.

Angular.module (' Notewrangler '). Factory (function  tweetablefactory ($http) {  var characterlength = 144;   return function (potentialtweet) {    return  $http ({      ' POST ',      '/http/ Gentle-spire-1153.herokuapp.com/tweet ',      data: {        description:potentialtweet,        maxLength: Characterlength      }   });

Change the factory definition into a provider definition.

function Tweetableprovider ($http) {

Wrap the existing function returned by we 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 function ($http) {    returnfunction(potentialtweet) {      return $http ({        ' POST ',        ' Http://gentle-spire-1153.herokuapp.com/tweet ',        data : {          description:potentialtweet, maxlength:characterlength}};};};  }]);

Create a setLength() function attached to the provider that sets the characterLength variable.

Angular.module (' Notewrangler '). Provider (' Tweetable ', [functionTweetableprovider () {varCharacterlength = 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 this our provider are ready for go, let's call the setLength() method of TweetableProvider Configure the acceptable maximum tweet length . Instead of 144 characters, we need to allow for a characterLength .

Let's call on our config() NoteWrangler module and provide it an anonymous function.

Inject into the TweetableProvider config() function.

Call the function of the from setLength() TweetableProvider within the function and config() pass it a value of.

Angular.module (' Notewrangler ', [' Ngroute ']). config (function(tweetableprovider) {    Tweetableprovider.setlength (+);});

link:https://code.google.com/p/crypto-js/

[AngularJS + Cryptojs + Gravatar] Provider vs Factory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.