Search code examples
javascriptangularjsangular-directive

AngularJS: How to access input[date] min attribute within custom directive?


I'm trying to get write access of <input type="date" min attribute within my custom directive value. As I know input[date] element is directive to. https://docs.angularjs.org/api/ng/input/input%5Bdate%5D So access by $(elem).attr('min') is not right way. How can I access input[date] min attribute within my directive?

JsFiddle here: http://jsfiddle.net/musuk/Lbbtyjod/


Solution

  • DEMO

    As per your directive:

    use attrs.myDirective to access minvalue inside your directive link function. To access min date: attrs.min and to set min date attrs.$set('min', '2015-03-02')

    .directive("myDirective", function(){
       return {
          require: 'ngModel',
           scope: {
               minValue: "=myDirective"
           },
          link: function(scope, element, attrs) {
              scope.$watch('minValue', function(){
                  console.log(attrs.myDirective);
                  // Set min here
                  attrs.$set('min', '2015-03-02');
              });
          }
        };
    });