- 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.
推薦閱讀
- C++面向?qū)ο蟪绦蛟O(shè)計(jì)(第三版)
- Learning Selenium Testing Tools with Python
- 零基礎(chǔ)學(xué)Scratch少兒編程:小學(xué)課本中的Scratch創(chuàng)意編程
- C#完全自學(xué)教程
- Python 3破冰人工智能:從入門到實(shí)戰(zhàn)
- C#程序設(shè)計(jì)
- 速學(xué)Python:程序設(shè)計(jì)從入門到進(jìn)階
- C++語言程序設(shè)計(jì)
- Mastering Apache Storm
- 從零開始學(xué)Android開發(fā)
- iOS開發(fā)項(xiàng)目化入門教程
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- AutoCAD基礎(chǔ)教程
- SQL Server on Linux
- Java設(shè)計(jì)模式深入研究