- 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.
推薦閱讀
- SQL for Data Analytics
- Learning Data Mining with R
- Linux Device Drivers Development
- 編程與類型系統(tǒng)
- 深入實踐Kotlin元編程
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- Getting Started with Python
- Advanced Python Programming
- Python機器學習與量化投資
- Deep Learning for Natural Language Processing
- AutoCAD基礎(chǔ)教程
- MATLAB 2020 GUI程序設(shè)計從入門到精通
- Clojure Web Development Essentials
- Data Manipulation with R(Second Edition)
- Learning D3.js 5 Mapping(Second Edition)