vaske
vaske

Reputation: 9542

AngularJS custom directive for datepicker do not update ng-model

I checked all open questions with similar topic but neither gives me proper working solution which drives me crazy and that is why I opening this question.

My custom directive looks like

angular.module('angularSampleApp') .directive('datepicker', function() {
'use strict';

return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, el, attr, ngModel) {
        $(el).datepicker({
            maxDate: 0 ,
            onSelect: function(dateText) {
                scope.$apply(function(){
                    ngModel.$setViewValue(dateText);
                    ngModel.$viewValue = dateText;
                    ngModel.$render();
                });
            }
        });
    }
  };
});

I use directive like this

<input type="text" id="datepicker" datepicker ng-model="jumpDate">
<a name="jump" id="jump" title="Jump" ng-click="jumpToDate()">Jump</a>

and in controller I have something like this:

    $scope.jumpToDate = function() {
      console.log($scope.jumpDate);
  };

However directive do not update $scope.jumpDate and it's always undefined even if I set default value in controller it's never updated, dateText have proper value.

I'm using angular build 1.4.1

Upvotes: 1

Views: 1284

Answers (1)

Cheyne
Cheyne

Reputation: 2119

I would check the scope values on the directive. I'm guessing that ng-model is setting the value on the isolate scope, not the controller.

Or try using

ng-model="$parent.jumpDate"

Upvotes: 1

Related Questions