0

I am using jquery ui date picker in angularjs and it is working fine. But it is not working when datepicker comes inside ng-repeat loop.

<input type="text" class="form-control date_picker" ng-model="date.date" ng-repeat="date in ot.dates">
//Directive
app.directive('otForm', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'app/components/ots/form.html',
        link: function ($scope, form) {
            $(".date_picker").datepicker({
                dateFormat: 'dd-mm-yy',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                showButtonPanel: false,
                changeYear: true,
                changeMonth: true
            });
        }
    }
})

How can I solve this?

3
  • Can you show your ng-repeat loop? - Added, thanks Commented Aug 4, 2015 at 8:59
  • it is there in text box Commented Aug 4, 2015 at 9:01
  • What exactly doesn't work? Do you see the inputs with the date value? Commented Aug 4, 2015 at 9:07

1 Answer 1

4

The issue is that your DOM will have multiple <input>s with the class of .date_picker and so your jQuery selector $(".date_picker") is going to return an increasing number of matches as the ng-repeat grows. You really just want your jQuery selector to match the one directive element in your ng-repeat. You are in luck because the link function in a directive passes you the scope and the element itself:

<input type="text" class="form-control date_picker" ng-model="date.date" ng-repeat="date in ot.dates">
//Directive
app.directive('otForm', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'app/components/ots/form.html',
        link: function (scope, element, attrs) {
            $(element).datepicker({
                dateFormat: 'dd-mm-yy',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                showButtonPanel: false,
                changeYear: true,
                changeMonth: true
            });
        }
    }
})

See the change to the link: line and the immediately following line. I assume you have your directive implemented correctly, but not shown here. You have restrict: 'E' but I don't see a <otForm> in your ng-repeat.

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

3 Comments

here element is not textbox. entire temploate
Perhaps you could change the directive to restrict: 'A' and add otForm as an attribute to your <input>: <input otForm type="text" ... > so that your directive only maps to the relevant element.
I created a directive for datepicker and now it is wokring

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.