0

I had datepicker using jquery. It works when the page is loaded because i set the default date in my controller. Picking a date is ok but the problem is when you get the value using angular js its not updated, always the default value. Anyone how to update the value when date is selected and set by jquery. Really appreciate your help and thanks in advance.

html

<input type="text" id="date1"  name="date1" ng-model="data.date1">
<input type="text" id="date2"  name="date2" ng-model="data.date2">
<button type="button" ng-click="get()">Test Value</button>

jquery

$('#date1').datepicker({
language: 'en',
autoClose:true,
dateFormat: 'd MM yyyy',
onSelect: function (fd, date, picker) {
}});

$('#date2').datepicker({
language: 'en',
autoClose:true,
dateFormat: 'd MM yyyy',
onSelect: function (fd, date, picker) {


}});

angular js

var app = angular.module('app', []);
app.controller('MainController', function($scope, $http){
 $scope.data = {};

 // default date
 $scope.data.date1  = new Date();
 $scope.data.date2  = moment(new Date()).add(1, 'days');

 $scope.get = function(){
    console.log($scope.data.date1);
    console.log($scope.data.date2);
 }});
5
  • better not to use jquery with angular. You will get different plugins for datepicker in angular itself. Commented Apr 28, 2018 at 5:54
  • use angular daptepicker. github.com/720kb/angular-datepicke Commented Apr 28, 2018 at 5:54
  • Thanks for the advice and links. Commented Apr 28, 2018 at 5:57
  • material.angularjs.org/latest/demo/datepicker Commented Apr 28, 2018 at 6:03
  • Thanks @VipulSolanki. I would try this. Commented Apr 28, 2018 at 6:31

1 Answer 1

1

If you are about to use datepicker more than one time, I suggest to create directive and just use it.

Directive

app.directive('datepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {
            $(function(){
                element.datepicker({
                    dateFormat:'dd/mm/yy',
                    onSelect:function (date) {
                        scope.$apply(function () {
                            ngModelCtrl.$setViewValue(date);
                        });
                    }
                });
            });
        }
    }
});

HTML

<input type="text" ng-model="date1" datepicker />
<input type="text" ng-model="date2" datepicker />

For more detail, just check this Plunker

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

3 Comments

i used this plugin t1m0n.name/air-datepicker/docs. not working on this plugin. thanks.
Just check this documentation for same functionality but with AngularJS. So no need to code into JQuery.
@Rye You check above given link if you want to continue with your datepicker plugin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.