- 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.
推薦閱讀
- Java面向?qū)ο蟪绦蜷_發(fā)及實(shí)戰(zhàn)
- Python應(yīng)用輕松入門
- Oracle BAM 11gR1 Handbook
- Effective Python Penetration Testing
- QGIS By Example
- AppInventor實(shí)踐教程:Android智能應(yīng)用開發(fā)前傳
- Visual FoxPro程序設(shè)計(jì)習(xí)題集及實(shí)驗(yàn)指導(dǎo)(第四版)
- 區(qū)塊鏈底層設(shè)計(jì)Java實(shí)戰(zhàn)
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- 21天學(xué)通C++(第5版)
- Rust游戲開發(fā)實(shí)戰(zhàn)
- JSP程序設(shè)計(jì)實(shí)例教程(第2版)
- MySQL程序員面試筆試寶典
- Managing Microsoft Hybrid Clouds
- Hack與HHVM權(quán)威指南