0

The following code work with pure angularjs correct, but when I integrate it to a Mobile Angular UI App it does not update the model after selecting a date. If I edit the input field without the date picker it works fine.

Template:

<div>
<label>{{myLabel}}:</label>
<input class="form-control datetime-picker" placeholder="{{myPlaceholder}}" ng-model="ngModel" required="required">
</div>

Directive js code:

(function(){
    var app = angular.module('MeetingApp.directives.HelpDirective', []);

    app.directive("datetimePicker", function() {
        return {
            restrict: 'E',
            templateUrl: "datetime-picker.html",
            scope: {
                ngModel: '=',
                myPlaceholder: '@',
                myLabel: '@'
            },
            require: ['?^ngModel'],
            link: function(scope, element, attrs, ngModelCtrl) {
                $(element).find('.datetime-picker').datetimepicker({
                    format: "dd.mm.yyyy hh:ii",
                    autoclose: true,
                    language: "de",
                    startDate: new Date(),
                    minuteStep: 10
                });
            }
        };
    });
})();

Usage:

<form method="post" id="meetingForm" ng-controller="MeetingController as meetingCtrl" novalidate>    
    <datetime-picker my-placeholder="Startzeit" my-label="Startzeit" ng-model="meetingCtrl.meeting.start"></datetime-picker>
    <datetime-picker my-placeholder="Endzeit" my-label="Endzeit" ng-model="meetingCtrl.meeting.end"></datetime-picker>
</form>

Github link: https://github.com/knobli/meetingApp

To run the app: npm install -g bower yo gulp generator-mobileangularui bower install gulp build gulp

knobli
  • 657
  • 1
  • 9
  • 18
  • @pankajparkar the element is a div element containing the label and input element. – knobli May 25 '15 at 08:53
  • 1
    do try `$(element).find('.datetime-picker').datetimepicker` looks like everytime you are initializing datepicker for all the element which has `datetime-picker` class – Pankaj Parkar May 25 '15 at 08:56
  • @pankajparkar thanks that works, but it does not solve the problem – knobli May 25 '15 at 09:05

2 Answers2

1

Whenever you are using any jQuery plugin you need to update your scope bindings manually, because any changes from outside angular context would not run angular digest cycle.

For updating ng-model on change of input you need to update an ng-model on its change event dp.change.

Directive

app.directive("datetimePicker", function() {
    return {
        restrict: 'E',
        templateUrl: "datetime-picker.html",
        scope: {
            ngModel: '=',
            myPlaceholder: '@',
            myLabel: '@'
        },
        require: ['?^ngModel'],
        link: function(scope, element, attrs, ngModelCtrl) {
            var dpElement = $(element).find('.datetime-picker');
            dpElement.datetimepicker({
                format: "dd.mm.yyyy hh:ii",
                autoclose: true,
                language: "de",
                startDate: new Date(),
                minuteStep: 10
            });
            dpElement.on('dp.change', function(event) {
                //need to run digest cycle for applying bindings
                scope.$apply(function() {
                    ngModelCtrl.$setViewValue(event.date);
                });
            });
        }
    };
});

Similar SO Answer Here with detailed explanation.

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

This require: ['?^ngModel'], looks strange to me... If you require a thing, in my opinion, it can't be optional, right?

Try using require: 'ngModel', and let me know if it works.

You need to use this:

dpElement.on('changeDate', function(ev) {
            //need to run digest cycle for applying bindings
            scope.$apply(function() {
                ngModelCtrl.$setViewValue(ev.date);
            });
        });

But be aware to not call your parameter event. This is a reserved word in JavaScript. Take a look at W3Schools

tBX
  • 174
  • 1
  • 10