官术网_书友最值得收藏!

Creating the Category Service

The CategoryService objects is a singleton object because it is an AngularJS service. The service will interact with our CMS APIs powered by the Spring Boot application.

We will use the $http service. It makes the HTTP communications easier.

Let's write the CategoryService:

(function (angular) {
'use strict';

/* Services */
</span> angular.module('cms.modules.category.services', []).
service('CategoryService', ['$http',
function ($http) {

var serviceAddress = 'http://localhost:8080';
var urlCollections = serviceAddress + '/api/category';
var urlBase = serviceAddress + '/api/category/';

this.find = function () {
return $http.get(urlCollections);
};

this.findOne = function (id) {
return $http.get(urlBase + id);
};

this.create = function (data) {
return $http.post(urlBase, data);
};

this.update = function (data) {
return $http.put(urlBase + '/id/' + data._id, data);
};

this.remove = function (data) {
return $http.delete(urlBase + '/id/' + data._id, data);
};
}
]);
})(angular);

Well done, now we have implemented the CategoryService

The .service function is a constructor to create a service instance, the angular acts under the hood. There is an injection on a constructor, for the service we need an $http service to make HTTP calls against our APIs. There are a couple of HTTP methods here. Pay attention to the correct method to keep the HTTP semantics.

主站蜘蛛池模板: 清原| 尼木县| 临江市| 榆林市| 永兴县| 中西区| 绥滨县| 曲沃县| 宣汉县| 漠河县| 彝良县| 深州市| 揭西县| 响水县| 溆浦县| 杂多县| 偏关县| 清水县| 黑河市| 奎屯市| 永平县| 开远市| 资阳市| 广河县| 大荔县| 裕民县| 麻江县| 高安市| 锦州市| 中山市| 兴海县| 和田市| 于都县| 台中县| 晋城| 平乐县| 哈巴河县| 延津县| 西林县| 怀安县| 永吉县|