0

I was trying to use ngReadonly withing directive but my code is not working properly:

app.directive('jqdatepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {



            $(function(){
                element.datepicker({
                    dateFormat:'mm/dd/yy',
                    onSelect:function (date) {

                            scope.$watch(attrs.ngReadonly, function(value) {

                                console.log(value);
                                scope.$apply(function () {
                                     ngModelCtrl.$setViewValue(value?"":date);
                                });

                        });


                    }
                });
            });
        }
    }
});

My datepicker should work according to check box, the ideal goal would be not to trigger datepicker at all, when the checkbox is unchecked.

THanks

InGeek
  • 2,532
  • 2
  • 26
  • 36

3 Answers3

1

Instead of using ng-readonly, I used ng-disabled

<input type="text" jqdatepicker  ng-disabled="!formData.gtdata.ch" ng-model="formData.gtdata.dt1"/>

app.directive('jqdatepicker', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link : function (scope, element, attrs, ngModelCtrl) {
                $(function(){
                    element.datepicker({
                        dateFormat:'mm/dd/yy',
                        onSelect:function (date) {
                            scope.$apply(function () {
                                 ngModelCtrl.$setViewValue(date);
                            });

                        }
                    });
                });
        }
    }
});
InGeek
  • 2,532
  • 2
  • 26
  • 36
0
<input type="text" jqdatepicker  ng-readonly ="!formData.gtdata.ch" ng-model="formData.gtdata.dt1"/>

    app.directive('jqdatepicker', function() {
        return {
            restrict: 'A',
            require : 'ngModel',
            link : function (scope, element, attrs, ngModelCtrl) {
                    $(function(){


          //disable datepicker
                if (attrs.ngReadonly || attrs.ngReadonly || attrs.Readonly)                 {
                    var str = attrs.ngReadonly || attrs.Readonly;
                    scope.$watch(str, function (newValue, oldValue) {
                        if (newValue) {
                            element.datepicker("option", "disabled", true);
                        } else {
                            element.datepicker("option", "disabled", false);
                        }
                    }, 0);

                }
                        element.datepicker({
                            dateFormat:'mm/dd/yy',
                            onSelect:function (date) {
                                scope.$apply(function () {
                                     ngModelCtrl.$setViewValue(date);
                                });

                            }
                        });
                    });
            }
        }
    });
Amin Uddin
  • 968
  • 6
  • 15
  • 31
0

Here is what I did for Jquery UI datepicker. The idea is to return false inside datepicker.beforeShow if datepicker ng-readonly is true. HTML

<input type="text" jqdatepicker ng-model="SomeDate " ng-value="SomeDate | date:'dd/MM/yyyy'" ng-readonly="ro"/>

Js

app.directive('jqdatepicker', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            scope: {
                ngReadonly: '=',
            },
            link: function (scope, element, attrs, ctrl) {
                element.datepicker({
                    onSelect: function (date) {
                        ctrl.$setViewValue(date);
                        ctrl.$render();
                        scope.$apply();
                    },
                    beforeShow: function () {
                        var canShow = !scope.ngReadonly;
                        return canShow;
                    },
                });
            }
        };
    });
andre719mv
  • 1,478
  • 13
  • 14