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

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.

主站蜘蛛池模板: 绍兴县| 德安县| 桂林市| 海宁市| 汪清县| 左贡县| 青州市| 湖州市| 普格县| 洛阳市| 焉耆| 大名县| 大竹县| 松滋市| 崇文区| 东方市| 辽宁省| 阿坝县| 麦盖提县| 丹棱县| 陇南市| 长白| 休宁县| 安仁县| 武山县| 嘉定区| 来凤县| 凤阳县| 永安市| 义马市| 陆河县| 濮阳市| 黄冈市| 龙岩市| 桦南县| 富锦市| 宿州市| 新巴尔虎左旗| 英山县| 沿河| 甘泉县|