Directive updates parent scope value - angularjs

I use bootstrap-datepicker for its ability to select a range of days. I try to put it in Angular directive and expect to update the parent scope value when the date changed, and the date format must be in the whole week (20-04-2015 - 26-04-2015)
var app = angular.module('angular.controls.weekDatePicker', [])
app.directive('weekDatePicker', ['$filter', '$parse', function ($filter, $parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
var ngModelParseFn = $parse(attrs.ngModel);
element.datepicker({
minViewMode: parseInt(attrs.minviewmode),
format: attrs.format,
language: "vi"
}).on('changeDate', function (e) {
scope.$apply(function () {
console.log(scope.$parent);
if (attrs.week == 'true') {
}
else {
// code
}
});
});
element.datepicker('update', new Date()); //reset to now
if (attrs.week == 'true') {
scope.$watch(function () {
return ngModelCtrl.$modelValue;
}, function (newValue) {
var date = element.data().datepicker.viewDate;
var startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
var endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
startDate = $filter('date')(startDate, 'dd-MM-yyyy');
endDate = $filter('date')(endDate, 'dd-MM-yyyy');
console.log(scope.$parent.fixtureDate); //fail to get parent scope value ?
var dateText = startDate + ' - ' + endDate;
ngModelParseFn.assign(scope, dateText);
});
}
scope.$on("$destroy",
function handleDestroyEvent() {
element.datepicker('remove');
}
);
}
};
}]);
View:
<input week-date-picker ng-model="fixtureDate" minviewmode="0" format="MM-yyyy" class="form-control" week="true" style="width:200px" />
Plunker source here

I've done a version with a callback (Plunker).
JS
element.datepicker({
minViewMode: parseInt(attrs.minviewmode),
format: attrs.format,
language: "vi"
}).on('changeDate', function(e) {
var date = e.date;
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1); // 0 = january
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var startDate = firstDay.getDate() + '-' + (firstDay.getMonth() + 1) + '-' + firstDay.getFullYear();
var endDate = lastDay.getDate() + '-' + (lastDay.getMonth() + 1) + '-' + lastDay.getFullYear();
scope.changeDate({startDate: startDate, endDate: endDate});
});
Note this in the directive;
scope: {changeDate: "&"},
index.html
$scope.fixtureDate = {
startDate: startDate,
endDate: endDate
};
$scope.updateFixtures = function(startDate, endDate) {
$scope.fixtureDate.startDate = startDate;
$scope.fixtureDate.endDate = endDate;
};

You should change the html to
<input week-date-picker ng-model="fixtureDate" minviewmode="0" format="MM-yyyy" class="form-control" week="true" style="width:200px" />
You have no variable called dt in your code. The variable that you want to change is called fixtureDate.

Related

Remove $watch from this directive

I've wrote this directive to handle my date inputs: https://plnkr.co/edit/7hpc8u5pVc7iaNSwn7Zw?p=preview
app.directive('myDate', ['$filter', myDate]);
function myDate($filter) {
var directive = {
restrict: 'E',
template: template,
require: 'ngModel',
scope: {},
link: link
}
return directive;
function template(element, attrs) {
var template = '<input ng-model="date" ng-keyup="keyup($event.keyCode)" ui-mask="99/99/9999" type="text" ';
if (attrs.class) {
template += 'class="' + attrs.class + '"';
element.removeClass(attrs.class);
}
template += '/>';
return template;
}
function link(scope, element, attrs, ctrl) {
scope.keyup = function(key) {
if (key === 68) { // D key
scope.date = $filter('date')(new Date(), 'ddMMyyyy');
}
};
ctrl.$formatters.push(function(data) { // model to view
data = $filter('date')(data, 'ddMMyyyy');
return data;
});
ctrl.$parsers.push(function(data) { // view to model
var year = data.toString().substr(-4);
var month = data.toString().substr(2, 2);
var day = data.toString().substr(0, 2);
var sep = '-';
data = (year && month && day) ? Date.parse(year + sep + month + sep + day) : '';
return data;
});
scope.$watch('date', function() {
ctrl.$setViewValue(scope.date);
});
ctrl.$render = function() {
scope.date = ctrl.$viewValue;
};
}
}
Unfortunately I used $watch to keep my model updated... I would like to know if there is a better way to trigger $setViewValue(scope.date) without $watch; just to optimize it a bit.
Thank you!
Add this to your template:
ng-change="updateParent()"
And this to you link:
scope.updateParent = function(){
ctrl.$setViewValue(scope.date);
}

Change the ngmodel value in Angularjsdatepicker directive

I have tried to convert a bootstrap datepicker from eternicode to angularjs directive and allow to select week mode. The problem is the value {{var1}} from the view doesn't reflect to the custom value that i set on Directive
weekDatePicker directive:
var app = angular.module('angular.controls', [])
app.directive('weekDatePicker', ['$filter','$parse', function ($filter, $parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
console.log('start');
element.datepicker({
//minViewMode: 1,
//format: "MM-yyyy"
}).on('changeDate', function (e) {
scope.$apply(function () {
var date = e.date;// element.datepicker('getDate');
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
startDate = $filter('date')(startDate, 'dd-MM-yyyy');
endDate = $filter('date')(endDate, 'dd-MM-yyyy');
dateText = startDate + ' - ' + endDate;
e.date = dateText;
//console.log(e.date);
ngModelCtrl.$setViewValue(dateText); //>> no change from view
console.log(ngModelCtrl);
});
});
}
};
}]);
You can see the whole code at jsFiddle
View: <input week-date-picker ng-model="var1" class="form-control" />
{{var1}} >> why it doesn't get the custom value from directive ?
I have forked you fiddle
var app = angular.module('angular.controls', [])
app.directive('weekDatePicker', ['$filter','$parse', function ($filter, $parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
console.log('start');
var ngModelParseFn = $parse(attrs.ngModel);
element.datepicker({
//minViewMode: 1,
//format: "MM-yyyy",
// language: "vi"
})
scope.$watch(function () {
return ngModelCtrl.$modelValue;
}, function (newValue) {
var date = element.data().datepicker.viewDate;
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
startDate = $filter('date')(startDate, 'dd-MM-yyyy');
endDate = $filter('date')(endDate, 'dd-MM-yyyy');
dateText = startDate + ' - ' + endDate;
ngModelParseFn.assign(scope, dateText);
});
}
};
}]);
Hope this is what you want.

ngModelController: update view value after parser executed

I have the following directive:
myDirectives.directive('simpleDate', function ($filter, $locale, $timeout) {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, elem, attrs, ctrl) {
var dateFormat = 'shortDate';
ctrl.$formatters.unshift(function (modelValue) {
if (!modelValue) {
return '';
} else {
return $filter('date')(modelValue, dateFormat);
}
});
ctrl.$parsers.unshift(function (viewValue) {
if (!isNaN(viewValue)) {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = Number(viewValue),
lastDay = (new Date(year, month, 0)).getDate();
if (day >= 1 && day <= lastDay) {
return '' + year + (month < 10 ? '-0' : '-') +
month + (day < 10 ? '-0' : '-') + day;
}
}
return '';
});
}
};
});
Parser calculates model value, and needs to reflect back view value; but how? I know there is ctrl.$render() method, but I guess it must run after $parser executed. The following didn't work:
$timeout(function () { ctrl.$render(); });
How can I render view value then?
Is this what you expect? :
myDirectives.directive('simpleDate', function ($filter, $locale, $timeout) {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, elem, attrs, ctrl) {
var dateFormat = 'shortDate';
ctrl.$formatters.unshift(function (modelValue) {
if (!modelValue) {
return '';
} else {
return $filter('date')(modelValue, dateFormat);
}
});
ctrl.$parsers.unshift(function (viewValue) {
if (!isNaN(viewValue)) {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = Number(viewValue),
lastDay = (new Date(year, month, 0)).getDate();
if (day >= 1 && day <= lastDay) {
var currentValue = '' + year + (month < 10 ? '-0' : '-') + month + (day < 10 ? '-0' : '-') + day;
ctrl.$setViewValue(currentValue);
ctrl.$render();
return currentValue;
}
}
return '';
});
}
};
});

AngularJS Directive failing apply because $apply is already running

Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.7/$rootScope/inprog?p0=%apply
Hi! I'm making a mini calendar standalone directive and I'm having trouble applying the changes of the months and the day. It tells me $apply is already in progress, I have google around and notice it's a problem when using it inside a compile definition object.
But preventing the $apply from running when it's already doing it didn't solve things, since it seems like its always running. Any suggestion or fixes for my issue would be appreciated :D
Controller + Directive
function calendarController($scope) {
$scope.config = new Date(2013, 4, 25);
}
angular.module("calendar", []).directive('miniCalendar', function($parse) {
return {
restrict: "E",
replace: true,
templateUrl: "../views/template.html",
transclude: true,
controller: function($scope) {
$scope.currentDate = new Date();
$scope.prev = function() {
$scope.mCalDate = new Date($scope.mCalDate.getFullYear(), $scope.mCalDate.getMonth() - 1, $scope.mCalDate.getDay());
$scope.updateDate($scope.mCalDate);
};
$scope.next = function() {
$scope.mCalDate = new Date($scope.mCalDate.getFullYear(), $scope.mCalDate.getMonth() + 1, $scope.mCalDate.getDay());
$scope.updateDate($scope.mCalDate);
};
$scope.selecting = false;
$scope.selectDate = function() {
$scope.selecting = !$scope.selecting;
};
$scope.selectDay = function(day) {
$scope.mCalDate = day.date;
$scope.updateDate($scope.mCalDate);
};
$scope.currentMonth = '';
$scope.currentYear = '';
$scope.days = [{
day: "Sun"
}, {
day: "Mon"
}, {
day: "Tue"
}, {
day: "Wed"
}, {
day: "Thu"
}, {
day: "Fri"
}, {
day: "Sat"
}];
$scope.weeks = [];
},
scope: {
mCalDate: '='
},
compile: function(element, attrs) {
var modelAccessor = $parse(attrs.mCalDate);
return function(scope, element, attrs, controller) {
scope.currentDate = scope.mCalDate.getDate() + '/' + Math.abs(scope.mCalDate.getMonth() + 1) + '/' + scope.mCalDate.getFullYear();
var firstDayOfMonth = new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), 1).toDateString().split(' ');
var totalDays = new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth() + 1, 0).getDate();
var firstDayIndex;
for (var i in scope.days) {
if (scope.days[i].day == firstDayOfMonth[0]) {
firstDayIndex = i;
}
}
var allDays = [];
var h = 0;
for (i = 0; i < totalDays + 6; i++) {
if (i >= firstDayIndex && (h + 1) <= totalDays) {
allDays.push({
dayName: scope.days[new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), h + 1).getDay()].day,
day: ++h,
date: new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), h + 1)
});
} else {
allDays.push({});
}
}
var calendar = []
var chunk = 7;
for (i = 0, allDays.length; i < allDays.length; i += chunk) {
calendar.push({
week: calendar.length,
days: allDays.slice(i, i + chunk)
});
}
scope.weeks = calendar;
scope.currentYear = scope.mCalDate.getFullYear();
scope.currentMonth = scope.mCalDate.toDateString().split(' ')[1];
scope.$watch('mCalDate', function(val) {
//debugger;
});
scope.updateDate = function(date) {
scope.mCalDate = date;
var phase = scope.$root.$$phase; //Safe applying
if (phase != '$apply' || phase != '$digest') {
scope.$apply(function() {
scope.mCalDate = date;
$parse(attrs.mCalDate).assign(scope.$parent, 'config');
});
}
};
};
}
};
});
HTML
<mini-calendar type="text" m-cal-date="config" />
Template
<div class="miniCalendar"><label ng-click="selectDate()"><input type="text" ng-model="currentDate" disabled></label><div ng-show="selecting"><div><a><span ng-click="prev()">Prev</span></a><a><span ng-click="next()">Next</span></a><div ><span ng-bind="currentMonth">January</span> <span ng-bind="currentYear"></span></div></div><table><thead><tr><th ng-repeat="day in days"><span ng-bind="day.day"></span></th></tr></thead><tbody><tr ng-repeat="week in weeks"><td ng-repeat="d in week.days"><a ng-bind="d.day" ng-click="selectDay(d)">1</a></td></tr></tbody></table></div></div>
Using $timeout instead of $apply can solve $digest issue.
scope.updateDate = function(date) {
$timeout(function() {
scope.mCalDate = date;
$parse(attrs.mCalDate).assign(scope.$parent, 'config');
});
};

Parent scope update by angularjs datepicker directive

I am developing a datePicker directive comprising 3 x selects (views/datetime.html)
<select class="form-control input-group-sm w75 float-left"
ng-model="mth"
ng-options="x for x in ['Jan','Feb','Mar','Apr','May','Jun']">
</select>
<select class="form-control input-group-sm w75 float-left"
ng-model="day"
ng-options="x for x in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]">
</select>
<select class="form-control input-group-sm w75"
ng-model="year"
ng-options="x for x in [2014,2015,2016,2017,2018]">
</select>
The directive can be used one or more time in each html view (startdate, enddate, etc)
<div class="input-group-sm" date-picker ng-model="startdate"></div>
My directive is coded as follows:
App.directive('datePicker', function () {
var date = {};
var objLink = {
restrict: 'A',
templateUrl: 'views/datetime.html',
scope: {
ngModel: '=',
},
link: function($scope, element, attr) {
$scope.$watch("mth", function(value) {
if ( typeof value != 'undefined' ) {
date.mth = value;
updateTarget($scope, attr);
}
});
$scope.$watch("day", function(value) {
if ( typeof value != 'undefined' ) {
date.day = value;
updateTarget($scope, attr);
}
});
$scope.$watch("year", function(value) {
if ( typeof value != 'undefined' ) {
date.year = value;
updateTarget($scope, attr);
}
});
}
};
return objLink;
function updateTarget( scope, attr ) {
var d = date.mth+'-'+date.day+'-'+date.year;
scope[attr.date] = d;
}
});
My issue is that function UpdateTarget() does NOT update the $scope.startdate in the controller.
Any ideas...anyone? Thanks in advance.
scope[attr.date] = d;
Here attr.date is undefined. To update the $scope.startdate in the controller, you need to pass attr.ngModel as stated below
scope[attr.ngModel] = d;
...Worked it all out myself! Thx for any feedback.
App.directive('datePicker', function () {
var mths = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var dirLink = {
restrict: 'A',
templateUrl: 'views/datetime.html',
scope: {
data : '='
},
link: function($scope, element, attr) {
// watch bound data item for change
// - ajax load delay from view render
$scope.$watch("data", function(value) {
if ( typeof $scope.data == 'undefined' ) {
return;
}
// initialise view controls
if ( typeof $scope.mth == 'undefined' ) {
var a = $scope.data.split(' ');
var dt = a[0].split('-');
$scope.mth = mths[dt[1]-1];
$scope.year = dt[0];
$scope.day = dt[2];
}
});
// watch view controls
$scope.$watch("mth", function(value) {
if ( typeof value != 'undefined' ) {
var mth = padl(mths.indexOf(value)+1);
updateTarget($scope, 'mth', mth);
}
});
$scope.$watch("day", function(value) {
if ( typeof value != 'undefined' ) {
updateTarget($scope, 'day', value);
}
});
$scope.$watch("year", function(value) {
if ( typeof value != 'undefined' ) {
updateTarget($scope, 'year', value);
}
});
}
};
return dirLink;
function updateTarget( scope, seg, val ) {
// init date if empty
if ( scope.data.length < 8 ) {
var d = new Date();
var dt = d.getYear()+'-'+padl(d.getMonth())+'-'+padl(d.getDate());
scope.data = dt;
}
// split existing data
var a = scope.data.split(' ');
var dt = a[0].split('-');
// change selected segment
var d = ['year','mth','day'];
dt[d.indexOf(seg)] = val;
// reassemble date
var d = dt[0]+'-'+dt[1]+'-'+dt[2];
scope.data = d;
}
function padl( n ) {
return ("0"+n).slice(-2);
}
});

Resources