1

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

3
  • You only need ngModel.$setViewValue(dateText) and you don't need to wrap it into $scope.$apply. Commented Jun 29, 2015 at 18:09
  • @zeroflagL still nothing tried that one... Commented Jun 29, 2015 at 18:21
  • possibly same as that of stackoverflow.com/a/29194068/2435473 Commented Jun 29, 2015 at 18:32

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

That creates a tight dependency doesn't it?

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.