I had datepicker using jquery. It works when the page is loaded because i set the default date in my controller. Picking a date is ok but the problem is when you get the value using angular js its not updated, always the default value. Anyone how to update the value when date is selected and set by jquery. Really appreciate your help and thanks in advance.
html
<input type="text" id="date1" name="date1" ng-model="data.date1">
<input type="text" id="date2" name="date2" ng-model="data.date2">
<button type="button" ng-click="get()">Test Value</button>
jquery
$('#date1').datepicker({
language: 'en',
autoClose:true,
dateFormat: 'd MM yyyy',
onSelect: function (fd, date, picker) {
}});
$('#date2').datepicker({
language: 'en',
autoClose:true,
dateFormat: 'd MM yyyy',
onSelect: function (fd, date, picker) {
}});
angular js
var app = angular.module('app', []);
app.controller('MainController', function($scope, $http){
$scope.data = {};
// default date
$scope.data.date1 = new Date();
$scope.data.date2 = moment(new Date()).add(1, 'days');
$scope.get = function(){
console.log($scope.data.date1);
console.log($scope.data.date2);
}});
If you are about to use datepicker more than one time, I suggest to create directive and just use it.
Directive
app.directive('datepicker', function() {
return {
restrict: 'A',
require : 'ngModel',
link : function (scope, element, attrs, ngModelCtrl) {
$(function(){
element.datepicker({
dateFormat:'dd/mm/yy',
onSelect:function (date) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(date);
});
}
});
});
}
}
});
HTML
<input type="text" ng-model="date1" datepicker />
<input type="text" ng-model="date2" datepicker />
For more detail, just check this Plunker
Related
I am new to Angularjs. I have put 2 Datepickers. I need that When I select date from first datepicker then the smaller dates from that date should be disabled in second Datepicker. I am unable to set "minDate" for second Datepicker directive.
Below is my code :
HTML Code :
<div ng-controller='TestController'>
<label>From Date :</label>
<input type="text" id="1" datepicker="" ng-init="variable=''"
ng-model="startDate" ng-change="test()" />
<label>To Date :</label>
<input type="text" id="2" callFuc="test()" datepicker1=""
ng-model="endDate" />
</div>
Js Code:
app.controller('TestController', function($scope, $window) {
$scope.test = function() {
$scope.variable = $scope.startDate;
$window.alert("From date is" + $scope.variable);
};
});
app.directive('datepicker', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$('#1').datepicker({
minDate: 0,
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
}
};
});
app.directive('datepicker1', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$('#2').datepicker({
minDate: scope.test(),
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
}
};
});
I am able to disable past dates in first Datepicker using minDate : "0".
But I am unable to set minDate properly for the second Datepicker. I am not able to fetch selected date from the First Datepicker and pass it to "datepicker1" directive. Any help or suggestion will be appreciated.
Update
I have updated code. Kindly review it.
https://plnkr.co/edit/ohCFzpwLGgFPPH49jybq?p=preview
The datepicker directive needs to watch for changes and update minDate as needed:
app.directive('datepicker1', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
scope.$watch(attr.minDate, function(value) {
el.datepicker({
minDate: value,
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
});
}
};
});
Usage:
<label>To Date :</label>
<input type="text" min-date="variable" datepicker1
ng-model="endDate" />
i am using this directive for datepicker
define(['./module'], function(directives) {
'use strict';
directives.directive('ngDate', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
$(element).datepicker({
dateFormat: 'dd-mm-yy',
onSelect: function(date) {
console.log(date);
ngModelCtrl.$setViewValue(date);
scope.$apply();
}
});
}
};
});
});
i have used it in html as follow
<input type='text' data-ng-model='fromDate' placeholder="dd-mm-yy" id="fromDate" data-ng-date='' readonly='readonly'>
<input type='text' data-ng-model='toDate' placeholder="dd-mm-yy" id="toDate" data-ng-date='' readonly='readonly'>
here from date should not be greater than todate. how to validate that in angularjs? second datepicker should not display dates before selected date in to datepicker ie if 26th April is selected in from datepicker than in to datepicker dates before 26th should not be displayed. how to achieve that?
Is there a way in angular JS to get Timestamp from a date obtained by a form?
<label for="start">From:</label>
<input type="date" ng-model="startDate" />
What will I have to write in the directive to transform this data? I didn't find anything about this particular issue,
Regards
Since you are using input type as date, the string in model should be compatible with Date object in javascript. So you can do
var timestamp = new Date($scope.startDate).getTime()
Use a directive to change the value from view to model.
http://jsfiddle.net/bateast/Q6py9/1/
angular.module('app', [])
.directive('stringToTimestamp', function() {
return {
require: 'ngModel',
link: function(scope, ele, attr, ngModel) {
// view to model
ngModel.$parsers.push(function(value) {
return Date.parse(value);
});
}
}
});
To use in a controller you can do something like this:
myApp.controller('TimestampCtrl', ['$scope', function($scope) {
$scope.toTimestamp = function(date) {
var dateSplitted = date.split('-'); // date must be in DD-MM-YYYY format
var formattedDate = dateSplitted[1]+'/'+dateSplitted[0]+'/'+dateSplitted[2];
return new Date(formattedDate).getTime();
};
}]);
And can be used like this:
<div ng-controller="TimestampCtrl">
The timestamp of <input ng-model="date"> is {{ toTimestamp(date) }}
</div>
I'm trying to use angular strap datepicker
When I select the date, it returns string like "2013-10-21T00:00:00.000Z"
But when I set this string to variable he doesn't react...
i tried this ways
$scope.var1="2013-10-14T00:00:00.000Z";
$scope.var1= {date: new Date("2013-10-14T00:00:00.000Z")};
$scope.var1="08/10/2012";
Here is plunker
How to send date to him?
And is there any way to interact with it? Or i must use only var1 assigned as his model?
The string like 2013-10-14T00:00:00.000Z represents by new Date().
To store form, you can do that by create custom directive:
directive
app.directive('datetimez', function() {
return {
restrict: 'A',
require : 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat:'dd/MM/yyyy',
}).on('changeDate', function(e) {
console.log(e.date);
ngModelCtrl.$setViewValue(e.date);
scope.$apply();
});
}
};
});
HTML
<input id="inputDatepicker" class="input-small" type="text" datetimez ng-model="var1" data-date-format="dd/mm/yyyy" >
See demo Plunker
Hopfully this is helpful to someone else... I just spent 20 min looking for this.
HTML:
<input type="date" id="date" ng-model="object.date">
CONTROLLER:
.controller('yourController', function($scope) {
$scope.object = {
date: new Date()
}
});
I have this html:
<form name="form" novalidate data-ng-submit="TradeDaysFormSubmit()">
<div data-ng-repeat="tradeDay in tradeDays">
{{ tradeDay }}
<br />
<div inline-calender class="trade-calender-box" date="tradeDay" data-ng-model="tradeDay"></div>
</div>
</form>
With this controller:
$scope.tradeDays = [];
$scope.tradeDays.push("31-01-2013");
$scope.tradeDays.push("28-02-2013");
$scope.tradeDays.push("10-03-2013");
$scope.tradeDays.push("11-04-2013");
$scope.tradeDays.push("12-05-2013");
$scope.tradeDays.push("13-06-2013");
$scope.tradeDays.push("11-07-2013");
$scope.tradeDays.push("23-08-2013");
$scope.tradeDays.push("14-09-2013");
$scope.tradeDays.push("30-10-2013");
$scope.tradeDays.push("22-11-2013");
$scope.tradeDays.push("23-12-2013");
$scope.tradeDays.push("31-01-2014");
And the directive:
app.directive("inlineCalender", function () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
date:'='
},
link: function (scope, element, attributes, modelController) {
scope.$watch('date', function (newValue) {
if (!_.isString(newValue)) return;
dateparts = scope.date.split('-');
var minDate = new Date(dateparts[2], parseInt(dateparts[1]) - 1, 1);
var maxDate = new Date(dateparts[2], parseInt(dateparts[1]), 0);
element.datepicker({ defaultDate: scope.date, hideIfNoPrevNext: true, minDate: minDate, maxDate: maxDate });
element.bind("change", function (event) {
var currentDate = element.datepicker("getDate");
scope.date = currentDate;
modelController.$setViewValue(currentDate.toString());
modelController.$render();
scope.$apply();
});
});
}
}
});
The view shows all my calenders, so far so good. But, when I change a date, the new value is not passed to the model. As you can see, in the HTML I have {{ tradeDay }}, but this value doesn't change when I select a date on my calender.
Can someone tell me what I am doing wrong here? I've tried the model controller, and isolate scope with two-way binding, but neither did work.
Fiddle When you click a date in the datepicker, the text above is not updated.
There is just a small mistake in your code you should be using dot in a model just change your code to below and it will work
http://jsfiddle.net/Qvu5u/3/
Just use dot model like
$scope.tradeDays.push({date:'31-01-2013'});
https://github.com/angular/angular.js/wiki/Understanding-Scopes
The best practice to deal with the isolated scope is always have a '.' in your ng-models.
You can remove the $watch to prevent $apply to trigger multiple times. (You can see that in your code if you log something before $apply)
The date change can be easily captured by the callback function of the calender, you can change the code to:
link: function (scope, element, attributes, modelController) {
dateparts = scope.date.split('-');
var minDate = new Date(dateparts[2], parseInt(dateparts[1]) - 1, 1);
var maxDate = new Date(dateparts[2], parseInt(dateparts[1]), 0);
element.datepicker({
defaultDate: scope.date,
hideIfNoPrevNext: true,
minDate: minDate,
maxDate: maxDate,
onSelect: function (date) {
modelController.$setViewValue(date);
scope.date = date;
scope.$apply();
}
});
}
Demo on jsFiddle