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

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.

主站蜘蛛池模板: 龙川县| 镇平县| 乌苏市| 通渭县| 汕头市| 景宁| 凌海市| 改则县| 读书| 莱芜市| 兴和县| 开远市| 祁门县| 东乌珠穆沁旗| 兰州市| 五常市| 长宁区| 孙吴县| 长宁区| 安溪县| 元朗区| 信阳市| 大余县| 车致| 平利县| 民权县| 五寨县| 凌云县| 西和县| 榆树市| 南康市| 神池县| 博兴县| 遵化市| 霍州市| 恩施市| 怀仁县| 天水市| 云和县| 龙里县| 嘉峪关市|