Error bind bootstrap datetimepicker to angular model
I have a problem when binding bootstrap datetimepicker to my model in angular. Initially everything works fine, the problem is that when I select the date and time using the control, the model is never updated. However, if I set the date input manually, then binding also works. This is my angular controller:
routerApp.controller('CalendarCtrl',function($scope){
$scope.dateValue = "01/01/2000 12:00";
$('.form_datetime').datetimepicker({
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 1
});
});
And this is my datetimepicker control:
<form action="" class="form-horizontal" role="form">
<fieldset>
<legend>Test</legend>
<div class="form-group">
<label for="dtp_input1" class="col-md-2 control-label">DateTime Picking</label>
<div class="input-group date form_datetime col-md-5" data-date="1979-09-16T05:25:07Z" data-date-format="dd/mm/yyyy hh:ii" data-link-field="dtp_input1">
<input class="form-control" size="16" type="text" data-ng-model="dateValue">
<span class="input-group-addon"><span class="glyphicon glyphicon-remove" ></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
<input type="hidden" id="dtp_input1" value="" /><br/>
</div>
Date: {{dateValue}}
</fieldset>
</form>
Any idea? Thanks in advance!
+3
source to share
2 answers
I experienced this problem too. My solution was to use angular-ui bootstrap library which contains native AngularJS date picker.
Demo: http://angular-ui.github.io/bootstrap/#/datepicker
Alternatively, you can use AngularStrap , which also has its own angular time / time set.
+1
source to share
Use your directive like this:
'use strict';
var app = angular.module('App', []);
app.directive('datetimepicker', function(){
return {
require: '?ngModel',
restrict: 'A',
link: function(scope, element, attrs, ngModel){
if(!ngModel) return; // do nothing if no ng-model
ngModel.$render = function(){
element.find('input').val( ngModel.$viewValue || '' );
}
element.datetimepicker({
language: 'it'
});
element.on('dp.change', function(){
scope.$apply(read);
});
read();
function read() {
var value = element.find('input').val();
ngModel.$setViewValue(value);
}
}
}
});
I get this code at http://embed.plnkr.co/Cj1KXL/
0
source to share