5

I'm trying to use jQuery datepicker in my AngularJS app, but I get the following error message:

jQuery datepicker with AngularJS: "TypeError: element.datePicker is not a function"

I'm using the code from this answer: jQuery ui datepicker with Angularjs

Here's a working fiddle from that same answer: http://jsfiddle.net/kevinj/TAeNF/2/

This is the complete code I'm using:

<html>
<head>
    <script src="/js/angular.min.js"></script>
    <script src="/lib/jquery/jquery-1.12.0.min.js"></script>
    <script src="/lib/jquery/jquery-ui-1.11.4.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/lib/jquery/jquery-ui-1.11.4.min.css">
</head>

<body ng-app="app">

<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, d  MM, yy',
                onSelect: function (date) {
                    scope.date = date;
                    scope.$apply();
                }
            });
        }
    };
});
</script>

<p><input type="text" ng-model="date" jqdatepicker></p>
<p>{{ date }}</p>

</body>
</html>

What on earth am I doing wrong? Thank you!

3 Answers 3

6

When you load AngularJS before jQuery, that will by default use jQLite which is smaller jQuery API which is been there inside Angular itself.

Basically you need to include jQuery before angular js to use jQuery api by default while querying DOM.

Otherwise you need to explicitly use $(element)

NOTE

Make sure both the jQuery library version should be same, jQuery & jQuery.ui both.

Demo Fiddle

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

4 Comments

Thank you for your response Pankaj, but unfortunately it's still the same. Just in case, I changed to online libraries, but no change. These are the libraries I'm using: code.jquery.com/jquery-1.12.0.min.js code.jquery.com/ui/1.11.4/jquery-ui.min.js codeorigin.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js
@MarcusEdensky check updated answer.Also make sure library versions should be same.. and use latest angular stable version which is 1.4.8
Thank you once again Pankaj, but it still doesn't work. I upgraded to Angular 1.4.9 (apparently stable) and using 1.12.0 of both jQuery and jQuery UI. Do you have any idea?
@MarcusEdensky could you provide me a fiddle or plunkr? so that I can help further
1

You need to add the JS files in the following order, mentioned below.

  1. jquery.js

  2. jquery-ui.js

  3. moment.js

  4. bootstrap-datetimepicker.js

  5. app.js (Your angular module reference)

  6. angular-bootstrap-datetimepicker-directive.js

I was facing the same issue, because I missed the bootstrap-datetimepicker.js file reference. Hope this helps all :)

Comments

0

Also Check if the jquery-ui is physically present and properly pointed in bower_components.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.