Prashant Gupta
Prashant Gupta

Reputation: 163

Angularjs. ng-model is not being updated in datepicker

I was trying to write jquery to open datepicker on text click using hidden input area. Also this is required at multiple places.

Following is my html.

<div class="date-format">
    <input class="hidden-date-picker" id="hidden-date1" type="hidden" ng-model="date1"/>
    <a href="" class="date-picker" id="date1">{{date1 | date: 'dd MMM yyyy a'}}</a>
</div>
<div class="date-format">
    <input class="hidden-date-picker" id="hidden-date2" type="hidden" ng-model="date2"/>
    <a href=""  class="date-picker" id="date2">{{date2 | date: 'dd MMM yyyy a'}}</a>
</div>

Following is javascript -

var app = angular.module('index-app', []);

app.controller('index-controller', function($scope) {
$scope.date1 = new Date(2016, 0, 1);
$scope.date2 = new Date();

$('.hidden-date-picker').each(function() {
    $(this).datepicker({
        changeYear: 'true',
        changeMonth: 'true',
        startDate: '07/16/1989',
        firstDay: 1,
        onSelect: function(dateText, inst) {
            console.log($scope.$eval($(this).attr('ng-model')));
            $scope.$eval($(this).attr('ng-model')).setTime(Date.parse(dateText));
            console.log($scope.date1);
        }
    });
});
$('.date-picker').each(function() {
    $(this).click(function (e) {
        $('#hidden-' + $(this).attr('id')).datepicker('show')
        e.preventDefault();
    });
});
});

It is clear from the console output that $scope.date1 is being modified on selecting the date from calender. But the change is not being reflected in the html side.

Upvotes: 2

Views: 5052

Answers (2)

Manveer Singh
Manveer Singh

Reputation: 357

Below code can help you. For HTML

<div class="input-group date mydatepicker" data-provide="datepicker">
    <input type="text" class="form-control" style="outline:none !important;" id="CancelDate"
    data-ng-model="CancelDate" data-ng-required="true" placeholder="Cancel Date" 
    data-ng-change="onChange()"
    data-ng-trim="true" readonly/>
    <div class="input-group-addon custumc_con">
        <i class="fa fa-calendar" aria-hidden="true"></i>
    </div>
</div>

For angular controller

$scope.onChange = function () {

//initialize today date
var today = new Date();

//set new date as next 10th day of today
var newDate = newDate.setDate(today.getDate() + 10);

//format date and set to angular model
$scope.CancelDate = newDate.format("MM/dd/yyyy");

// set new date in datepicker of bootstrap
    $('.mydatepicker').datepicker('update', $scope.CancelDate);
}

Upvotes: 0

Shabaz S
Shabaz S

Reputation: 192

Using a directive can resolve your problem of ng-model not updated

Your html code

<input class="hidden-date-picker" id="hidden-date1" type="hidden" ng-model="date1" customzdatetime />

Add a directive 'customzdatetime' to your input tag.

app.directive('customzdatetime', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datetimepicker({
                debug: false,
                format: 'DD-MM-YYYY',
                maxDate: moment()
            }).on('dp.change', function (e) {
                ngModelCtrl.$setViewValue(e.date);
                scope.$apply();
            });
        }
    };
});

Upvotes: 4

Related Questions