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

  • Spring 5.0 By Example
  • Claudio Eduardo de Oliveira
  • 202字
  • 2021-06-24 19:17:39

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.

主站蜘蛛池模板: 喀喇| 望江县| 兴化市| 清水县| 怀仁县| 菏泽市| 陇西县| 阳城县| 准格尔旗| 萨迦县| 民丰县| 连平县| 齐河县| 赤水市| 平遥县| 威信县| 海阳市| 抚远县| 老河口市| 会宁县| 株洲市| 长春市| 黄冈市| 孟村| 普宁市| 浦城县| 凤山县| 巫溪县| 都匀市| 大丰市| 炎陵县| 内江市| 云龙县| 兴安县| 罗城| 安吉县| 全南县| 泸溪县| 东阿县| 维西| 衡阳市|