I am new to Angularjs and working on a pet project.
I have a main page which has functions in HomeController. Then I have a button in the main page which on click opens a modal and the modal has a separate controller called ModalController. There is also a directive which is present in the modal which has its own controller. I have 3 other places where I need to use date picker so I created a date picker directive and using it in all 3 places.
I am not able to do the below functionalities.
- Click on
editI opened a dialog but I need to open it with the row data and when pressed save should update the row. (The trouble I am facing here is how to get the value of the date which is directive's controller.) - Open Modal and enter the details and click save I need to create a new record and add it to the records which is present in the main controller. (How save the data from Modal controller to Home controller records.)
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