1

I am trying to get an AngularJS directive that uses jQuery-UI to work in jsfiddle.

  • jQuery-UI works fine when used directly
  • AngularJS works
  • but the directive reports: "element.datepicker is not a function"

What do I need to do to get jQuery-UI to work inside the directive in jsFiddle?

http://jsfiddle.net/edwardtanguay/oqo2xv3e/1

HTML

<div ng-app="mainApp">
    <div ng-controller="mainController">
        <div>{{message}}</div>
        <input id="normal" type="text" />
        <input id="startDate" type="text" ng-model="date" jqdatepicker />
    </div>
</div>

JavaScript

angular.module('mainApp', [])
.controller('mainController', function($scope) {
    $scope.message = 'test111';
    $('#normal').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();
                }
            });
        }
    };
});

1 Answer 1

1

Just wrap your directive with $()..

Because jQuery loads after angular so by default angular picks jqLite.

or loads jQuery first in script tag the angularjs.. you select angular from default lib in fiddle and add jQuery as external.

$(element).datepicker({
                dateFormat: 'DD, d  MM, yy',
                onSelect: function (date) {
                    scope.date = date;
                    scope.$apply();
                }
            });
Sign up to request clarification or add additional context in comments.

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.