Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chapter8/datepicker/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ app.controller('MainCtrl', function($scope) {
$scope.currentDate = '';
$scope.updateMyText = function(date) {
$scope.myText = 'Selected';
console.log('date=currentDate: ', date, $scope.currentDate);
};
});
15 changes: 6 additions & 9 deletions chapter8/datepicker/datepicker.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
angular.module('myApp.directives', [])
.directive('datepicker', function() {
.directive('datepicker', function($parse) {
return {
// Enforce the angularJS default of restricting the directive to
// attributes only
restrict: 'A',
// Always use along with an ng-model
require: '?ngModel',
// This method needs to be defined and passed in from the
// passed in to the directive from the view controller
scope: {
select: '&' // Bind the select function we refer to the right scope
},
link: function(scope, element, attrs, ngModel) {
if (!ngModel) return;

// the "select" argument defines which function the directive
// should call when a new date is selected via the UI
var selectFn = $parse(attrs.select);
var optionsObj = {};

optionsObj.dateFormat = 'mm/dd/yy';
Expand All @@ -27,9 +24,9 @@ angular.module('myApp.directives', [])

optionsObj.onSelect = function(dateTxt, picker) {
updateModel(dateTxt);
if (scope.select) {
if (selectFn) {
scope.$apply(function() {
scope.select({date: dateTxt});
selectFn(scope, {date: dateTxt});
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion chapter8/datepicker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</head>

<body ng-controller="MainCtrl">
<input id="dateField" datepicker ng-model="$parent.currentDate" select="updateMyText(date)">
<input id="dateField" datepicker ng-model="currentDate" select="updateMyText(date)">
<br/>
{{myText}} - {{currentDate}}
</body>
Expand Down