【发布时间】:2015-11-13 07:36:45
【问题描述】:
我是 Angularjs 的新手,正在从事一个宠物项目。
我有一个在HomeController 中具有功能的主页。然后我在主页上有一个按钮,单击它会打开一个模态,并且该模态有一个名为ModalController 的单独控制器。模态中还有一个指令,它有自己的控制器。我还有 3 个地方需要使用日期选择器,所以我创建了一个日期选择器指令并在所有 3 个地方使用它。
我无法执行以下功能。
- 点击
edit我打开了一个对话框,但我需要用行数据打开它,按下保存时应该更新行。 (我在这里遇到的麻烦是如何获取作为指令控制器的日期的值。) - 打开 Modal 并输入详细信息并单击保存 我需要创建一个新记录并将其添加到主控制器中存在的记录中。 (如何将数据从 Modal 控制器保存到 Home 控制器记录。)
script.js
angular.module('myApp', ['ngAnimate', 'ui.bootstrap']);
angular.module('myApp').controller('HomeController', function($scope, $uibModal) {
$scope.records = [{'date': new Date(), 'place': 'Bangalore'}];
$scope.openModal = function() {
$uibModal.open({
templateUrl: 'modaldialog.html',
controller: 'ModalController'
});
};
// How to get the directive date-picker value and pass it and save it.
$scope.edit = function(record) {
$uibModal.open({
templateUrl: 'modaldialog.html',
controller: 'ModalController'
});
};
$scope.addwithinCtrl = function() {
var record = {'date': new Date(), 'place': 'Hyderabad'};
$scope.records.push(record);
};
});
angular.module('myApp').controller('ModalController', function($scope, $uibModalInstance) {
// save the input and dismiss the dialog
$scope.save = function() {
// how to save the entered data before closing the dialog?
$uibModalInstance.dismiss('cancel');
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
});
angular.module('myApp').directive('dateDirective', function() {
return {
restrict: 'A',
templateUrl: 'date.html',
controller: function($scope) {
$scope.format = 'dd-MMM-yy';
$scope.open = function() {
$scope.status.opened = true;
};
$scope.status = {
opened: false
};
}
}
});
Plunker:PLUNKER
【问题讨论】:
-
我检查了您的代码,但不明白您为什么将所有 angular-ui 指令与您的代码一起包装。你能告诉我你为什么要为 datepicker 写另一个指令,因为你已经在里面使用了 uib-datepicker?
-
@wickY26 我检查了你的代码,但不明白你为什么用你的那些包装所有 angular-ui 指令我不明白,因为我是这里的初学者。你能详细说明一下吗?
标签: javascript angularjs twitter-bootstrap