Angularjs methods for obtaining service port data---$http, $resource, Restangular

Source: Internet
Author: User

Angularjs methods for obtaining service port data (three): $http, $resource, Restangular. In addition breeze can also refer to (ODATA)

Where: $http, with restangular return promise.

Promise is an object that takes a function as the then property value:

Then (Fulfilledhandler, ErrorHandler, Progresshandler)

After adding Fulfilledhandler, ErrorHandler, and Progresshandler, the Promise object is formed. Fulfilledhandler is called when promise is loaded with data, ErrorHandler is called when promise fails, and Progresshandler is invoked when the progress event is triggered. All parameters are optional, and non-function parameters are ignored. Sometimes Progresshandler is not just an optional parameter, but the progress event is a purely optional argument. The implementation of the promise pattern does not necessarily call Progresshandler every time (because it can be ignored), and only when this parameter is passed in will it occur.

This method returns a new Promise object after the Fulfilledhandler or ErrorHandler callback is completed. In this way, the promise operation can form a chained call. The return value of the callback handler is a Promise object. If the callback throws an exception, the returned Promise object will set the state to fail.

==========================

Use the built-in service $http of Angulasrjs to get server-side data examples in the controller :

var app = Angular.module (' Metronicapp ');

App.controller(' Appctrl ', function Appctrl ($scope, $http, restangular) {

(function getusers () {
var webapi = "/api/userss"; Access to the server where the program resides, omitting DNS, and access to its domain requires a full address.
$http. Get (WEBAPI)//Get data relative to the address
. Success (function (response, status, headers, config) {//Processing on success
var data = new Wijmo.collections.ObservableArray (response);
$scope. Users = new Wijmo.collections.CollectionView (data);
})
. Error (Function (response, status, headers, config) {//processing on failure, status on state code 403, etc.!
Console.log (' request error: ' + Webapi);
alert (page);
});

});
})();

}

============

Same service-side access using: Restangular

var app = Angular.module (' Metronicapp ');

App. Config (restangularprovider) {//configure the underlying routing path
Restangularprovider.setbaseurl ('/api ');
});

App.controller (' Appctrl ', function Appctrl ($scope, restangular) {//inject restangular service

(function getusers () {//This functions immediately executed, you can omit ...

Restangular.one ("Users"). GetList (). Then (function (data) {

$scope. Users = new Wijmo.collections.CollectionView (data);
}, function (err) {
Console.log (' request error: ' + err);
Alert ("Request error-----okkkkkk!");
});


})();

}

Angularjs methods for obtaining service port data---$http, $resource, Restangular

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.