Application of services in the ANGULARJS project

Source: Internet
Author: User

/**
* General Ajax Request Public service
*/
Mainmodule.factory (' MyService ', function ($http, $q) {
var service = {};
var baseUrl = '.. /‘;
var _url = ';
var _finalurl = ';
var _token = ';

/**
* Process Request Path
*/
var makeurl = function () {
_url = _url.split ("). Join (' + ');
_finalurl = BaseUrl +_url;
Return _finalurl
}

/**
* Set Request Path
*/
Service.seturl = function (URL) {
_url = URL;
}

Service.settoken = function (token) {
_token = token;
}

/**
* GET Request Path
*/
Service.geturl = function () {
return _url;
}

Service.gettoken = function () {
return _token;
}

/**
* $q. Defer () the role of several methods of constructing a deffered instance.
* If the asynchronous operation succeeds, the state of the Promise object is changed to "success" (that is, from pending to resolved) using the Resolve method, or if the asynchronous operation fails,
* Use the Reject method to change the state to "failed" (that is, from pending to rejected). Finally we return to deferred.promise and we can call the then method in a chain.
*/
Service.callituns = function () {
Makeurl ();
by calling the $q. Defferd returns the Deffered object as a chained call
/**
* Methods for Deffered objects
* 1.resolve (value): at declaration resolve (), indicates that the Promise object is transformed from pending state to resolve. Success Status
* 2.reject (reason): at declaration resolve (), indicates that the Promise object is transformed from pending state to rejected. Failure status
* 3.notify (value): at declaration notify (), indicates the Promise object unfulfilled state, which can be called multiple times before resolve or reject.
*/
var defrred = $q. Defer ();
$http ({
Method: ' Post ', url: _finalurl,headers: {' token ': _token}
}). Success (function (RESP) {
if (resp.success) {
Defrred.resolve (RESP);
}
})
/**
* Return Promise Object
* 1.then (ErrorHandler, Fulfilledhandler, Progresshandler):
The then method is used to listen for different states of a promise. ErrorHandler Monitor failed status,
* Fulfilledhandler Monitor fulfilled status, Progresshandler monitor unfulfilled (not completed) status.
* In addition, the notify callback may be called 0 to several times, providing a progress indication before resolving or rejecting (resolve and rejected).
* 2.catch (Errorcallback)--promise.then (NULL, errorcallback) shortcut
* 3.finally (callback)--allows you to see whether a promise is being executed or rejected, but does not modify the last value value.
* This can be used to do work that frees up resources or cleans up useless objects, whether promise is rejected or resolved. For more information, see the full documentation specification.
*/
return defrred.promise;
}

return service;
});

/* Apply the service in the controller */

/**
* Some JS file
*/
Mainmodule.controller (' Togetherctrl ', [' $scope ', ' $http ', ' $timeout ', ' $state ', ' MyService ',
function ($scope, $http, $timeout, $state, MyService) {
Set Request Path
var _url = ' kcsz/getcourselist.do '
Set method Setting Path
Myservice.seturl (_url);
Call Request Callback
/**
* Because Callitunes returns a Promise object, once our promise satisfies the
* We can call the. Then () method and set $scope.data.artistdata.
* will notice that our controller is very "thin". Because all of our logical and persistent data is stored in the service and not in the controller.
*/
Myservice.callituns (). Then (function (data) {
$scope. courserlist = Data.data;
Console.log (data);
});
}]);

Application of services in the ANGULARJS project

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.