admin管理员组

文章数量:1291871

I know how to detect mousedown event on a directive that is clicked. However my directive also needs to bee unforcused or deselected when mouse is down outside of my directive/ element. How can I do this?

I know how to detect mousedown event on a directive that is clicked. However my directive also needs to bee unforcused or deselected when mouse is down outside of my directive/ element. How can I do this?

Share Improve this question asked May 16, 2014 at 4:34 Dr.KnowitallDr.Knowitall 10.5k24 gold badges87 silver badges136 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

Create a link function in the directive which binds a mousedown event handler on the document. Then, bind another mousedown event on the directive element itself. The latter handler should also call event.stopPropagation() to prevent the event from bubbling all of the way up to the document level:

link: function(scope, elem, attrs){
  angular.element(document).bind('mousedown', function(){
    console.log('clicked on document');
  });

  elem.bind('mousedown', function(e){
    e.stopPropagation();
    console.log('clicked on directive');
  });
}

Working Plunker

本文标签: javascriptDetect Mousedown Event in AngularjsStack Overflow