Monday 15 March 2010

How to detect clicks outside of scope in angularjs -


I have an angle controller, which needs to be reset when the user clicks outside the controller's scope. How do i do this

Sample html:

  & lt; Div id = 'parent' & gt; & Lt; Div id = '1' ng-controller = "ctrl1" & gt; & Lt ;! - Other Things - & gt; & Lt; / Div & gt; & Lt; Div id = '2' ng-controller = "ctrl2" & gt; & Lt ;! - Other Things - & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div id = "parent2" & gt; & Lt ;! - Other Things - & gt; & Lt; / Div & gt;   

I want to be able to reset ctrl2 if a click is outside of ctr2 , when click goes out of div2

Define the ctr2 reset function

If you want a reset, when a cop is clicked within the div, So the fact that the controllers share the $ scope , this would be quite straightforward. If you want to set the "reset" div in the form of a click anywhere on the page

, the direction of the "reset" div, then one click wants to reset the div Handler:
  app.directive ('reset', function ($ window) {return} {template: '
I In '2' + - Click to red click me, I'm going to general '+' & lt; / div & gt; ', controller: function ($ scope) {$ scope.directiveToggler = true;}, link on Click anywhere to do : Work (area, element) {var w = angular.element ($ window); w.bind ('click', function (e) {if (e.target! = Element [0]. Children [0]) {Scope.directiveToggler = false; Scope. $ Apply ();} Other {scope.directiveToggler = true; scope. $ Apply ()}}}}}})

Note that Perhaps there are better ways to handle class / style changes compared to what I established, but the question was about click events;).



No comments:

Post a Comment