Search code examples
angularjsangularjs-directivepopoverangular-bootstrap

Angular- In directive, element validation always undefined


I have a form , which is having few input fields. I want to display error message in popover when user focus out the input field or click on submit button. I created one directive which adds the popover attributes to the input fields. So when user moving out from the input field I will check validation, if validation fails want to show the popover. But when I try to check always I am getting undefined.

Can someone help me on this. My plunker

app.directive("errorTooltip", function($compile, $interpolate, $timeout) {
  return {
    scope: true,
    require: 'ngModel',
    link: function (scope, element, attr, ctrl) { 
          element.attr('popover-trigger', "'show'"); 
          element.attr('popover-placement', 'top');
          element.attr('uib-popover', element.attr("data-info"));

          var blurred = false;
          element.on("blur",function(event){
           blurred = true;
          });
          scope.$watch(function() {
            console.log(ctrl.$name.$invalid); //always comes undefined
            element.triggerHandler('show');
            return ctrl.$name.$invalid
          }, function(invalid) {
          if (!blurred && invalid) { 
              return
              }
             console.log("test")
              //element.toggleClass('has-error', invalid);

        });


    }
  };
});

Thanks in advance .


Solution

  • Replace the line:

    console.log(ctrl.$name.$invalid);
    

    With this:

    console.log(ctrl.$invalid);
    

    Updated plunker here.