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

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.

主站蜘蛛池模板: 金门县| 中山市| 郧西县| 太原市| 池州市| 枝江市| 分宜县| 邹平县| 杨浦区| 高州市| 都匀市| 广德县| 招远市| 赣榆县| 绩溪县| 海伦市| 稷山县| 沙田区| 天津市| 肇庆市| 郎溪县| 昌图县| 黔西| 贡嘎县| 衡阳县| 界首市| 文山县| 泸西县| 房产| 阜康市| 亳州市| 刚察县| 奉贤区| 施甸县| 克什克腾旗| 无极县| 利辛县| 鹤山市| 海原县| 江都市| 屏南县|