- 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.
推薦閱讀
- Java應用與實戰
- Python Game Programming By Example
- Django:Web Development with Python
- Bootstrap Essentials
- Data Analysis with Stata
- GeoServer Beginner's Guide(Second Edition)
- Keras深度學習實戰
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎卷)
- 計算機應用基礎教程(Windows 7+Office 2010)
- Android應用開發深入學習實錄
- 創意UI:Photoshop玩轉APP設計
- Oracle 12c從入門到精通(視頻教學超值版)
- Python物理建模初學者指南(第2版)
- 計算機系統解密:從理解計算機到編寫高效代碼