3

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!!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer. This looks fine, but i need a datetimepicker, where i can select date and time at once, not separated controls.
Can I mix reglr Bootstrap and just the datepicker from AngularJS ?I'm having the same problem, but I dont want to replace bootstrap from the whole project at this moment.
0

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.