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

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.

主站蜘蛛池模板: 清丰县| 玉溪市| 秦皇岛市| 连州市| 邵东县| 仪征市| 涿州市| 丹棱县| 闽清县| 民和| 运城市| 泌阳县| 三江| 孝感市| 安顺市| 肃北| 杭锦旗| 泰顺县| 汝城县| 新和县| 浮梁县| 会昌县| 从江县| 辰溪县| 安阳市| 铜鼓县| 建平县| 垣曲县| 秦安县| 客服| 高平市| 正镶白旗| 房山区| 余庆县| 商水县| 泗阳县| 临西县| 富宁县| 黔江区| 平舆县| 新乡市|