title: Using $setValidity on an element outside of the current scope
body: <p>I have a global directive, that is in charge of getting the error message from the controller, compiling, and displaying it.</p>
  
  <p>In case there is a server validation error such as 
  <code>This email already exists</code>, I would like to focus on this element, and set it validity to false, e.g. <code>$setValidity(false)</code>.</p>
  
  <p>This directive is not the form, and it doesn't contain the form.</p>
  
  <p>What would you suggest (already tried everything which is commented out)</p>
  
  <pre><code>directive('messageCompile', function ( $compile, $window, $rootScope ) {
    return {
      restrict: 'A',
      scope: true,
      link: function ( scope, element, attrs ) {
        var el;
  
        attrs.$observe( 'template', function ( tpl ) {
          if ( angular.isDefined( tpl ) ) {
            // compile the provided template against the current scope
            el = $compile( tpl )( scope );
            // stupid way of emptying the element
            element.html("");
  
            // add the template content
            element.append( el );
          }
        });
        attrs.$observe('focus', function(val){
          if ( angular.isDefined( val )  &amp;&amp; Boolean(val)) {
            var el = angular.element('[name="' + attrs.focus + '"]');
            var form = el.parents().find('form');
            var formName = form.attr('name');
             el.focus();
            // scope[formName].$setValidity(val, false);       
            // el.scope().$setValidity(false);
            // scope[formName][val].$setValidity(false);
            //$rootScope.scope[formName][val].$setValidity(false);
            //$rootScope.scope[formName].$setValidity(val, false);
          }
        });
          var windowEl = angular.element($window);
          windowEl.on('scroll', function() {
            if(window.scrollY &gt; 46){
              element.parent().parent().addClass('stayTop');
  
              // alert('here');
            }  
            else{
              // alert('here2');
              element.parent().parent().removeClass('stayTop');
            }
          });
  
      },
    }
  }).
  </code></pre>