JQuery ui datepicker with Angularjs

I want to use jQuery UI datepicker using AngularJS.

I have a sample, but my code does not work.

Example:

http://www.abequar.net/jquery-ui-datepicker-with-angularjs/

My code is:

<input id="sDate" name="programStartDate" type="text" datepicker required/> angular.module('elnApp') .directive('datepicker', function () { return { restrict: 'A', require : 'ngModel', link : function (scope, element, attrs, ngModelCtrl) { $(function(){ element.datepicker({ dateFormat:'yy-mm-dd', onSelect:function (date) { ngModelCtrl.$setViewValue(date); scope.$apply(); } }); }); } } }); 

The TypeError: Object [object Object] has no method 'datepicker' error is displayed TypeError: Object [object Object] has no method 'datepicker' .

+45
jquery angularjs jquery-ui
Aug 09 '13 at 9:58 am
source share
14 answers

I have almost the same code as you and my work.

Do you have jQueryUI.js included in the page?

There is a violin here

 <input type="text" ng-model="date" jqdatepicker /> <br/> {{ date }} var datePicker = angular.module('app', []); datePicker.directive('jqdatepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element.datepicker({ dateFormat: 'DD, d MM, yy', onSelect: function (date) { scope.date = date; scope.$apply(); } }); } }; }); 

You will also need the ng-app = "app somewhere in your HTML

+41
Aug 26 '13 at 15:16
source share
 angular.module('elnApp') .directive('jqdatepicker', function() { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { $(element).datepicker({ dateFormat: 'dd.mm.yy', onSelect: function(date) { ctrl.$setViewValue(date); ctrl.$render(); scope.$apply(); } }); } }; }); 
+28
Apr 7 '14 at 12:46 on
source share

As a best practice, especially if you have several date picker mechanisms, you should not hard-code the variable name of the item's region.

Instead, you should get the ng-model input onSelect and update its corresponding scope variable inside the onSelect method.

 app.directive('jqdatepicker', function() { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ngModelCtrl) { $(element).datepicker({ dateFormat: 'yy-mm-dd', onSelect: function(date) { var ngModelName = this.attributes['ng-model'].value; // if value for the specified ngModel is a property of // another object on the scope if (ngModelName.indexOf(".") != -1) { var objAttributes = ngModelName.split("."); var lastAttribute = objAttributes.pop(); var partialObjString = objAttributes.join("."); var partialObj = eval("scope." + partialObjString); partialObj[lastAttribute] = date; } // if value for the specified ngModel is directly on the scope else { scope[ngModelName] = date; } scope.$apply(); } }); } }; }); 

EDIT

To fix the problem caused by @Romain (Nested Elements), I changed my answer

+11
Feb 10 '14 at 15:49
source share

Finally, I was able to run the datepicker directive in angular js, here are the pointers

include the following js in order

  • jquery.js
  • Jquery-ui.js
  • angular -min.js

I added the following

 <script src="http://code.jquery.com/jquery-1.10.2.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"> </script> 

in html code

 <body ng-app="myApp" ng-controller="myController"> // some other html code <input type="text" ng-model="date" mydatepicker /> <br/> {{ date }} //some other html code </body> 

in js, make sure you first code for the directive, and then add code for the controller, otherwise it will cause problems.

date picker directive:

 var app = angular.module('myApp',[]); app.directive('mydatepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element.datepicker({ dateFormat: 'DD, d MM, yy', onSelect: function (date) { scope.date = date; scope.$apply(); } }); } }; }); 

directory code mentioned above.

After this directive write the controller

 app.controller('myController',function($scope){ //controller code }; 

READ THIS INSTEAD in angular js

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> 

along with jquery.js and jquery-ui.js

we can implement angular js datepicker as

 <input type="date" ng-model="date" name="DOB"> 

This gives a built-in datepicker, and the date is set in the ng model and can be used for further processing and verification.

I discovered this after a lot of successful struggle with the previous approach. :)

+5
Nov 25 '15 at 10:59
source share

onSelect does not work in ng-repeat, so I made a different version using event binding

HTML

 <tr ng-repeat="product in products"> <td> <input type="text" ng-model="product.startDate" class="form-control date-picker" data-date-format="yyyy-mm-dd" datepicker/> </td> </tr> 

script

 angular.module('app', []).directive('datepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element.datepicker(); element.bind('blur keyup change', function(){ var model = attrs.ngModel; if (model.indexOf(".") > -1) scope[model.replace(/\.[^.]*/, "")][model.replace(/[^.]*\./, "")] = element.val(); else scope[model] = element.val(); }); } }; }); 
+4
Nov 25 '13 at 8:04
source share

Unfortunately, the viscount's answer did not work for me, so I was looking for another solution that is just as elegant and works for nested attributes in the ng model. It uses $ parse and accesses the ng model through attrs in the binding function, rather than requiring:

 myApp.directive('myDatepicker', function ($parse) { return function (scope, element, attrs, controller) { var ngModel = $parse(attrs.ngModel); $(function(){ element.datepicker({ ... onSelect:function (dateText, inst) { scope.$apply(function(scope){ // Change binded variable ngModel.assign(scope, dateText); }); } }); }); } }); 

Source: ANGULAR.JS BINDING TO JQUERY UI (DATEPICKER example)

+4
Apr 04 '15 at 16:32
source share

I changed the code and wrapped the update update inside $ apply ().

 link: function (scope, elem, attrs, ngModelCtrl){ var updateModel = function(dateText){ // call $apply to update the model scope.$apply(function(){ ngModelCtrl.$setViewValue(dateText); }); }; var options = { dateFormat: "dd/mm/yy", // handle jquery date change onSelect: function(dateText){ updateModel(dateText); } }; // jqueryfy the element elem.datepicker(options); 

}

working fiddle - http://jsfiddle.net/hsfid/SrDV2/1/embedded/result/

+3
Nov 24 '13 at 12:55
source share

The best article I found is the following: http://www.abequar.net/posts/jquery-ui-datepicker-with-angularjs

It took 20 minutes to integrate it with my page.

+3
Jun 03 '15 at 13:03
source share

Here is my code -

 var datePicker = angular.module('appointmentApp', []); datePicker.directive('datepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { $(element).datepicker({ dateFormat: 'dd-mm-yy', onSelect: function (date) { scope.appoitmentScheduleDate = date; scope.$apply(); } }); } }; }); 
+1
Oct 17 '14 at 10:34
source share
 myModule.directive('jqdatepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element.datepicker({ dateFormat: 'dd/mm/yy', onSelect: function (date) { var ar=date.split("/"); date=new Date(ar[2]+"-"+ar[1]+"-"+ar[0]); ngModelCtrl.$setViewValue(date.getTime()); // scope.course.launchDate = date; scope.$apply(); } }); } }; }); 

HTML code:

 <input type="text" jqdatepicker ng-model="course.launchDate" required readonly /> 
+1
Oct. 14 '15 at 15:15
source share

The core Index.html file for Angular can use the body tag as ng-view. Then all you have to do is include the script tag at the bottom of any page that gets pulled into Index.html using Angular as follows:

 <script type="text/javascript"> $( function() { $( "#mydatepickerid" ).datepicker({changeMonth: true, changeYear: true, yearRange: '1930:'+new Date().getFullYear(), dateFormat: "yy-mm-dd"}); }); </script> 

Why compromise things?

+1
Sep 23 '16 at 21:31
source share

I had the same problem and it was solved by placing links and inclusions in the following order:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> <link href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> 

 var datePicker = angular.module('app', []); datePicker.directive('jqdatepicker', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { element.datepicker({ dateFormat: 'dd/mm/yy', onSelect: function (date) { scope.date = date; scope.$apply(); } }); } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> <link href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> <body ng-app="app"> <input type="text" ng-model="date" jqdatepicker /> <br/> {{ date }} </body> 
0
Feb 03 '17 at 18:57
source share
 var selectDate = element.datepicker({ dateFormat:'dd/mm/yy', onSelect:function (date) { ngModelCtrl.$setViewValue(date); scope.$apply(); } }).on('changeDate', function(ev) { selectDate.hide(); ngModelCtrl.$setViewValue(element.val()); scope.$apply(); }); 
0
Jun 30 '17 at 13:20
source share

I think the module declaration is missing an Angular ust bootstrap dependency, for example:

 angular.module('elnApp', ['ui.bootstrap']) 

See the doc for Angular -ui-bootstrap.

-3
Aug 09 '13 at
source share



All Articles