Angular's $http service

Source: Internet
Author: User



Usage
$http (config);


Arguments


Config

Object describing the request to is made and how it should is processed. The object has following Properties:method–{string}–http method (e.g. ' get ', ' POST ', etc) url–{string| Trustedobject}–absolute or relative URL of the resource is being requested; Or an object created by a call to $sce. Trustasresourceurl (URL). Params–{object.<string| Object>}–map of strings or objects which'll is serialized with the Paramserializer and appended as get parameters. Data–{string| Object}–data to is sent as the request message Data.


Example

var req = {method
 : ' POST ',
 URL: ' http://example.com ',
 headers: {
   ' content-type ': Undefined
 },
 data: {test: ' Test '}
}

$http (req). Then (function () {...}, function () {...});


Excerpted from angular1.x official website: https://docs.angularjs.org/api/ng/service/$http



The Settings object can contain the following primary keys:

①method

Can be: get/delete/head/jsonp/post/put

②url: Absolute or relative request target
③params (String map or object)
The value of this key is a string map or object that is converted to a query string appended to the URL. If the value is not a string, it is serialized by JSON.

?

④data (string or object)

This object contains data that will be sent to the server as a message body. Typically used when sending a POST request.

Starting with Angularjs 1.3, it can also send binary data in a POST request. To send a Blob object, you can simply pass it by using the data parameter.

?


Using $http in the controller

var app = Angular.module (' app ', [' Ngtouch ', ' Ui.grid ', ' ui.grid.resizeColumns ', ' ui.grid.moveColumns ']);
 
App.controller (' Mainctrl ', [' $scope ', ' $http ', function ($scope, $http) {
  $scope. gridoptions = {
    enablesorting : True,
    columndefs: [
      {field: ' Name ', minwidth:200, width:250, enablecolumnresizing:false},
      {field: ' Gen Der ', Width: ' 30% ', maxwidth:200, minwidth:70},
      {field: ' Company ', Width: ' 20% '}
    ]
  };
 
  $http. Get ('/data/100.json ')
    . Success (function (data) {
      $scope. gridoptions.data = data;
    });

Excerpt from Angular UI grid:http://ui-grid.info/docs/#/tutorial/204_column_resizing



Get the data and filter the data

  $http. Get ('/data/100.json ')
    . Success (function (data) {
      Data.foreach (function setgender (row, index) {
        Row.gender = row.gender=== ' male '? ' 1 ': ' 2 ';
      });
 
      $scope. gridoptions.data = data;}
])
. Filter (' Mapgender ', function () {
  var genderhash = {
    1: ' Male ',
    2: ' Female '
  };
 
  return function (input) {
    if (!input) {return
      ";
    } else {return
      genderhash[input];
  }};
});





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.