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

  • 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.

主站蜘蛛池模板: 六盘水市| 旬阳县| 泰来县| 广德县| 福贡县| 德令哈市| 深州市| 双鸭山市| 乳山市| 开原市| 铁岭县| 新营市| 昌吉市| 隆德县| 房山区| 永善县| 庐江县| 武清区| 阜康市| 伊川县| 衢州市| 大连市| 民和| 宁南县| 德兴市| 苗栗市| 昌宁县| 涿鹿县| 什邡市| 吉林省| 百色市| 上蔡县| 麟游县| 肇州县| 平江县| 兴安县| 阳信县| 上栗县| 互助| 北宁市| 绥中县|