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 More articles about

  1. angularjs in provider,factory,service The difference and usage of

    angularjs in provider,factory,service The difference and usage of Can provide service, But there are differences service Instantiate when first injected , Instantiate only once , The whole application life cycle is a singleton ...

  2. AngularJS Medium Provider People :Service and Factory And so on

    introduction After reading a lot of articles, I may not be able to say AngularJS Several of them create suppliers (provider) Methods (factory(),service(),provider()) What's the difference , When and what to use , I used to be stupid ...

  3. AngularJS thorough (5)——provider

    It's wonderful , I have to quote it in full . To this level , Maybe I dare to say I understand ... http://syaning.com/2015/07/21/dive-into-angular-5/ In the use of AngularJS When , ...

  4. Build your own provider (Provider = Strategy + Factory Method) Design patterns - Provider Pattern( Provider Pattern )

    Build your own provider (Provider = Strategy + Factory Method)   1.1.1 Abstract In daily system design , We may have heard of the provider model , Even use it almost every day , stay .NET F ...

  5. 【5min+】 The confusion of design patterns ?Provider vs Factory

    Series introduction [ Five minutes dotnet] It's a way to use your fragmented time to learn and enrich .net Blog series of knowledge . It contains .net All aspects that may be involved in the system , such as C# Small details of ,AspnetCore, In microservices .net ...

  6. [ translate ]AngularJS Some of them Providers(Factory, Service, Provider) The difference between

    original text : http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/ What is? Provider? Angula ...

  7. Go deep into AngularJS—— Custom service details (factory、service、provider)

    Preface 3 There are two ways to create custom services .  Factory Service Provider You should know ,AngularJS It was invented by backstage staff after work , He mainly applied the idea of layering that existed in the background for a long time . So we have to understand the next point ...

  8. AngularJS In service serivce,factory,provider The difference between

    Angular A service is a singleton object created by a service factory . These service factories are made up of  service provider Created in turn . and service providers It's a constructor . They have to contain a $get Properties are used in instances ...

  9. angularjs model.service vs provider vs factory?

    <!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...

Random recommendation

  1. ajax request cookie Validation issues

    There is a problem in the discussion , Namely : An asynchronism initiated by the background in the browser ajax request , When the server responds , Incidental cookie Information , When subsequent requests are made to other pages under the same domain name , The cookie Whether it works , Will be submitted with the request to web The server ...

  2. One a day Linux command (16):which command

    We often linux To find a file , But I don't know where it is , You can use some of the following commands to search for : which   View the location of the executable . whereis  Check the location of the file . locate    Cooperate with the database to view the file location ...

  3. java JDK8 Learning notes —— The first 17 Chapter Reflection and classloader

    Chapter 17 Reflection and classloader 17.1 Using reflection Reflection :.class The document reflects the basic information of the class , from Class etc. API The way to get class information is called reflection . 17.1.1 Class And .class file 1.java.lang ...

  4. kinect for windows - SkeletonBasics-D2D One of the details

    Previous articles introduced the acquisition of depth maps , But the depth map just provides some data , These data are used by the upper application or the lower application , We hope that on this basis , Get some information , Like hand waving , People running , Or sports and so on , So this article begins with kin ...

  5. JVM Study ②

    JVM Operating mechanism 1.JVM Start process Java Start command -> Loading configuration search jvm.cfg-> Look for JVM.dll(JVM The main implementation )-> initialization JVM, get JNIEnv Interface 2.JVM The base ...

  6. CentOS7 Configure firewall

    Use the command to configure ##Add firewall-cmd --permanent --zone=public --add-port=/tcp ##Remove firewall-cmd --perma ...

  7. jquery Batch submit form values And batch setting form values

    $('#frmCustomerConfirmCar').find('[name]').each(function () { var type = $(this)[0].nodeName.toLower ...

  8. img Tag insert picture back 403, Browser can open directly

    Reference resources :https://segmentfault.com/q/1010000011752614/a-1020000011764026 Blog Garden introduces external pictures to appear , appear 403 problem , It's supposed to be a security chain , Will detect access ...

  9. Page loading exception Clear browser static files js css cache js Dynamic loading js css file , You can configure the file after dropout , Prevent browser caching

    js Several ways to clear the browser cache - Old rabbit Basha - Blog Garden https://www.cnblogs.com/Mr-Rocker/p/6031096.html js Several ways to clear the browser cache   One .CSS and ...

  10. P2852 [USACO06DEC] Milk mode Milk Patterns

    link This is a template problem of suffix matching We just need to put height Figure out Then divide the answer in half #include<cstdio> #include<algorithm> #inc ...