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

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.

主站蜘蛛池模板: 高要市| 板桥市| 石泉县| 玉屏| 德江县| 乌审旗| 美姑县| 河北省| 潮安县| 长岭县| 民权县| 工布江达县| 全椒县| 华蓥市| 连山| 竹山县| 冷水江市| 白河县| 石柱| 达拉特旗| 贺兰县| 河津市| 吉林省| 濮阳县| 淅川县| 杭锦旗| 南开区| 汽车| 贡山| 滦南县| 酉阳| 盐池县| 岳池县| 泰宁县| 远安县| 鄂托克前旗| 永吉县| 玉溪市| 赫章县| 兴安盟| 白玉县|