. , JQuery-UI Angularjs
"2015-03-24T04: 00: 00"
, , .
var date = "2015-03-24T04:00:00"
var formattedDate = date.match(/[\d-]+/).pop();
...
link: function( scope, element, attrs, input ){
element.datepicker( optionsObjectHere );
setInitialDateFormatOnInput();
function setInitialDateFormatOnInput(){
setTimeout(function(){
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
}
}
jquery .
HTML
<input type="text" class="inline" ng-model="inputValue" my-calendar-popup="calendarOptions" />
calendarOptions
var calendarOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
app.directive('myCalendarPopup', function(){
var defaultOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
return {
require:'?ngModel',
restrict: 'A',
link: function( scope, element, attrs, input ){
if ( !input ){ return; }
element.datepicker( createCalendarOptions());
setInitialDateFormatOnInput();
function createCalendarOptions(){
if( !attrs.rsCalendarPopup ){ return addRequiredJqueryFunction( defaultOptions );}
return formatOptions();
}
function formatOptions() {
var options = scope.$eval( attrs.rsCalendarPopup );
return addRequiredJqueryFunction( options );
}
function addRequiredJqueryFunction( options ){
options.onSelect = changeDate;
return options;
}
function changeDate( date ){
input.$setViewValue( date );
}
function setInitialDateFormatOnInput(){
setTimeout(function(){
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
}
}
}
});
Note. I can get this to work only in the displayed configuration. I added it to the controller and even to the controller directive and was unable to recreate the initial date condition. I did not find out why this is so far. Perhaps this solution only works in the link function, which is built into another isolated area directive and works because of a certain time.
source
share