Reputation: 3226
i'm having a problem while binding bootstrap datetimepicker to my model in angular. Initially everything works fine, the problem is that when i select a date and time using the control, then the model never is updated. However if i set the date input manually then the binding works too. 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!!
Upvotes: 3
Views: 985
Reputation: 1
Use your directive this way:
'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 in http://embed.plnkr.co/Cj1KXL/
Upvotes: 0
Reputation: 2290
I too have experienced this problem. My solution was to use the angular-ui bootstrap library which contains a native AngularJS date picker.
Demo: http://angular-ui.github.io/bootstrap/#/datepicker
Alternatively you could use AngularStrap which also has an angular native date/time picker.
Demo: http://mgcrea.github.io/angular-strap/##datepickers
Upvotes: 1