Is there a way to write an angular directive (or something angular-y) to affect all input or textarea?

StackOverflow https://stackoverflow.com/questions/23639925

문제

I'm looking to effect the blur on all of the textareas and input elements on a page. The idea is that when these element's are blurred from I can easily adjust other areas on the page. What comes to mind is event delegation like jQuery live() or on(). However, I don't want to include the full jQuery library, and I don't think that the jQuery lite version supports live() and on() is included, but I'm not seeing blur bubble up to the body so internally propagation might be squelched to input and text areas since they are actually angular directives under the hood.

Is there a way to handle global events for elements, like blur on inputs and textareas?

도움이 되었습니까?

해결책

You could write a directive like so:

app.directive('textarea', function() {
  return {
    restrict: 'E',
    link: function(scope, elem, attrs) {
      elem.blur(function() {
        // Things are getting blurry!.. seriously though, execute blur logic here
      });
    }
  };
});

You could also add one for inputs and then condense your blur logic into a service.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top