Solved

AngularJS HttpProviderTransform Request Shorthand?

Posted on 2014-04-19
4
419 Views
Last Modified: 2014-04-21
I found this very nice article concerning AngularJS and Web Api 2 here...

Q. Why is there so much code in the 'app.config(['$httpProvider' ?

Q. Does my code below really do the same thing?

The following code works great, just much smaller code footprint.

app.config(['$httpProvider', function ($httpProvider)
    {
        $httpProvider.defaults.transformRequest = function (obj)
        {
            var str = [];

            for (var p in obj)
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));

            return str.join("&");
        }
    }]);

Open in new window

0
Comment
Question by:WorknHardr
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 250 total points
ID: 40011735
Q1. The  $httpProvider.defaults.transformRequest in the article does more.

It processes an object where the values are also objects, or where the values are arrays, or a flat object like the expectation in the code you have above.

it does some type checking at the bottom to either parse the object if an object is provided, or just return the data provided.

Q2. Your code will work no problem as long as the data you provide is a non-empty object which contains just key value pairs - no nested objects. As long as you know that your app only needs to process this type of object, you're good to go. Nicer smaller footprint it is. You can always add more functionality as your project grows.

Hope that helps.
0
 

Author Comment

by:WorknHardr
ID: 40012424
Great explanation!
0
 

Author Closing Comment

by:WorknHardr
ID: 40013381
thx
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40013436
:)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question