- Spring 5.0 By Example
- Claudio Eduardo de Oliveira
- 184字
- 2021-06-24 19:17:39
Creating the Category Controller
Now, we need to create our controllers. We will start with the simplest to make the example more easy to understand. The CategoryController has the responsibility of controlling the data of the Category entity. There are two controllers, one enables us to create a category, and another lists all categories stored in the database.
The category-controller.js should be like this:
(function (angular) {
'use strict';
// Controllers
angular.module('cms.modules.category.controllers', []).
controller('CategoryCreateController',
['$scope', 'CategoryService','$state',
function ($scope, CategoryService,$state) {
$scope.resetForm = function () {
$scope.category = null;
};
$scope.create = function (category) {
CategoryService.create(category).then(
function (data) {
console.log("Success on create Category!!!")
$state.go('categories')
}, function (err) {
console.log("Error on create Category!!!")
});
};
}]).
controller('CategoryListController',
['$scope', 'CategoryService',
function ($scope, CategoryService) {
CategoryService.find().then(function (data) {
$scope.categories = data.data;
}, function (err) {
console.log(err);
});
}]);
})(angular);
We have created an AngularJS module. It helps us to keep the functions organized. It acts as a kind of namespace for us. The .controller function is a constructor to create our controller's instances. We received some parameters, the AngularJS framework will inject these objects for us.
推薦閱讀
- Learning Real-time Processing with Spark Streaming
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- 摩登創客:與智能手機和平板電腦共舞
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- PLC編程及應用實戰
- 微信小程序項目開發實戰
- R語言與網絡輿情處理
- Java編程的邏輯
- 深入淺出React和Redux
- 深度學習:Java語言實現
- 寫給程序員的Python教程
- Building Serverless Web Applications
- Exploring SE for Android
- CodeIgniter Web Application Blueprints
- JavaScript編程精解(原書第2版)