1

I'm using a jQuery-ui datapicker and have defined a directive (credit: http://www.abequar.net/posts/jquery-ui-datepicker-with-angularjs) to instantiate it. I'd now like to add an html attribute that defines the function to call to determine whether a day should be selectable in the datepicker. How would I do this?

The js:

//Directive for showing the jquery-ui datepicker
myApp.directive('datepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {
            $j(function(){
                //Instantiate the datepicker
                element.datepicker({
                    dateFormat:'dd/mm/yy',
                    beforeShowDay:function(date) {
                        //TODO Call function defined in attrs, passing the date object to it
                        theDefinedFunction(date)
                    },
                    onSelect:function (date) {
                        ngModelCtrl.$setViewValue(date);
                        scope.$apply();
                    }
                });
            });
        }
    }
});

The html:

<input type="text" datepicker show-days="myShowDaysFunction()" />

The myShowDaysFunction() would be defined in the controller.

(Edit) - Controller function:

$scope.myShowDaysFunction = function(date) {
    console.log("I get called"); //Logs to the console
    console.log(date); //Logs undefined to the console
}

Thanks.

1 Answer 1

1

You need to create an isolate scope on your directive and take the function as a scope variable.

myApp.directive('datepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        scope: {
            showDays: '&'
        },
        link : function (scope, element, attrs, ngModelCtrl) {
            $j(function(){
                //Instantiate the datepicker
                element.datepicker({
                    dateFormat:'dd/mm/yy',
                    beforeShowDay:function(date) {
                        //TODO Call function defined in attrs, passing the date object to it
                        scope.showDays({date: date});
                    },
                    onSelect:function (date) {
                        ngModelCtrl.$setViewValue(date);
                        scope.$apply();
                    }
                });
            });
        }
    }
});

Markup

<input type="text" datepicker show-days="myShowDaysFunction(date)" />

Controller Function

$scope.myShowDaysFunction = function(date) {
    alert(date);
}

Plunker Exmaple

http://plnkr.co/edit/kRc76icPUa9qTkPH4UKm?p=preview

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

5 Comments

Thanks. The function in my controller scope now gets called, but the date object is not passed to it. A console.log(date) in the beforeShowDay callback logs the date, but in the controller function it's undefined. Any clues?
Does your controller function have a date param? Could you post the controller function please?
I've added the controller function.
Look at my answer, I have made the edit to show you how to receive the date on the controller function.
NP glad I could help!

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.