1

I am working on getting a jQuery UI date picker to work inside of an AngularJS 1.3 directive. So far I have been able to get the date picker to render on the screen, and for the selected value to be displayed within the text box. However, I am not able to pass the selected value back to the parent controller for use within in the application.

Here is my directive code.

angular.module("demo", []).directive('myDatepicker', function ($parse) {
   return {
      restrict: "E",
      replace: true,
      transclude: false,
      compile: function (element, attrs) {
         var modelAccessor = $parse(attrs.ngModel);
     var html = "<input type='text' id='" + attrs.id + "' >" +
        "</input>";

     var newElem = $(html);
     element.replaceWith(newElem);

     return function (scope, element, attrs, controller) {

        var processChange = function () {
           var date = new Date(element.datepicker("getDate"));

           scope.$apply(function (scope) {
              // Change bound variable
              modelAccessor.assign(scope, date);
           });
        };

        element.datepicker({
           inline: true,
           onClose: processChange,
           onSelect: processChange
        });

        scope.$watch(modelAccessor, function (val) {
           var date = new Date(val);
           element.datepicker("setDate", date);
        });

     };

  }

}; });

5
  • 1
    angular-ui.github.io/bootstrap there is datepicker here, u can customize it if u need Commented Mar 20, 2015 at 16:18
  • I have looked at using there directive; however, I have not been able to get it work with AngularJS 1.3 . There website claims that it works with AngularJS 1.2 The ultimate goal I have is to be able to use any jQuery UI component in an Angular Directive. Commented Mar 20, 2015 at 16:22
  • 1
    Thats interesting, because on that site there is cute link: Edit in Plunker, where u can change link to angular from 1.2 to 1.3.1 or 1.3.4 and it works... Commented Mar 20, 2015 at 16:43
  • Thanks, Petr I will check it out. Commented Mar 20, 2015 at 16:48
  • Petr, I got it to work inside my application. Thanks for the help Commented Mar 20, 2015 at 17:14

2 Answers 2

1

Based on your comments, I suggest that you bring in angular material. They have a great date picker for use with Angular 1.3:

Angular Material Form with Date Picker Demo

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

1 Comment

Thanks Sam. I didn't know that this existed.
0

If you want to use selected value within the application remember

the selected value to be displayed within the text box

Get the value of input, it has the selected value of datepicker according to your words.

I suggest to read more about AngularJS, you will find out using $watch and $apply is not recomended for this kind of "simple" even "trivial" operations.

1 Comment

The problem is that I do not have access to the value that is displayed inside of the input field. The pages controller does not see the value. This is the issue that I am having. Your answer does nothing to actually help me figure out how to fix the issue that I am having.

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.