7

When trying to add a directive to a input with a dynamic id, the link method does not properly bind to the object. Given the following jsfiddle or html:

<div ng-app="myApp" ng-controller="MyCtrl">
  <p>Date: <input type="text" id="datepicker-{{id}}" datepicker></p>    
</div>

And js:

var module = angular.module('myApp', []);

module.directive('datepicker', function() {
  var linker = function(scope, element, attrs) {
    element.datepicker();
  }

  return {
    restrict: 'A',
    link: linker
  }
});

function MyCtrl($scope) {
  $scope.id = 7
}

What I see on the console debugger is that when link is called it is showing the id literally as "datepicker-{{id}}" and not "datepicker-7".

Is there a way to force this to happen? A better way to implement this?

Update: Should have clarified. The datepicker shows up when clicked, but clicking on a date does not function. I get the error: "Uncaught Missing instance data for this datepicker"

1 Answer 1

14

I believe you need transclude:true in your directive return object, which tells angular to preprocess the markup for things like {{ }} bindings.

You also need to wrap the call to datepicker() in a $timeout to delay the attempt until the next angular cycle runs, ensuring that the translcuded ID is set in the DOM.

This worked for me in jsfiddle

var module = angular.module('myApp', []);

module.directive('datepicker', function($timeout) {
    var linker = function(scope, element, attrs) {
        $timeout( function(){
            element.datepicker();
        });
    }

    return {
        restrict: 'A',
        link: linker,
        transclude: true
    }
});

function MyCtrl($scope) {
    $scope.id = 7
}
7
  • The id on the inspect element tab still shows 'datepicker={{id}}' Nov 1, 2013 at 19:40
  • Should have clarified. The datepicker shows up when clicked, but clicking on a date does not. This still doesn't get that working. I get the error: "Uncaught Missing instance data for this datepicker"
    – craineum
    Nov 1, 2013 at 19:43
  • @DeepankarBajpeyi interesting, the id shows as datepicker-7 for me are you sure the jsfiddle you're looking at has transclude on it?
    – Brian
    Nov 1, 2013 at 19:44
  • 2
    @craineum sorry, I missed that part of your question. I added a $timeout wrapper around where you call datepicker, which seemed to work. Will update the answer. From what I understand, $timeout defers calling the inner function until the next angular cycle. I think without it, datepicker() was being called before angular had transcluded the element ID. Wrapping it in $timeout makes sure a proper ID is set before attempted to datepicker-ize it.
    – Brian
    Nov 1, 2013 at 19:52
  • 2
    That was it. With that you also don't need the transclude and could remove that from your answer. Awesome! Thanks!
    – craineum
    Nov 1, 2013 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.