3

I am developing small web page for mobile devices with Angular. I want to toggle click listener for whole page like

$('.page').on('click',...);
$('.page').off('click',...);

but in Angular way. And I don't want to use ng-Click with some conditions because most of the time the click should not work, it is just for error handling. Is it possible or I need to user pure js/jQuery?

Panel HTML:

<div class="alert error" ng-show="errorMessage">
    <div class="alert-text">{{ errorMessage }}</div>
</div>
6
  • In what conditions you need to bind/unbind the event listener? Commented Jan 30, 2014 at 13:34
  • If some error happens then panel with error description will be shown and click listener should be bind. After click the panel will be disappear and event should be unbind. Commented Jan 30, 2014 at 13:40
  • So this is something that is only related to this panel. can you show the markup of this panel? Commented Jan 30, 2014 at 13:42
  • Of course, if it is only related with the panel I'd just add ngClick on this panel. But I have requirement to close the panel after click on any place in page Commented Jan 30, 2014 at 13:47
  • just show your panel html Commented Jan 30, 2014 at 13:50

2 Answers 2

2

I created a directive:

  • It should be used in conjunction with ngIf.
  • ngIf should refer to an assignable variable on the scope
  • For example don't do this: ng-if='myFunc()' or ng-if='var1 || var2'

here is a plunker: http://plnkr.co/edit/fmNH2PbRrruRqGFYiui1?p=preview

Directive:

app.directive('errorBox', function($document, $parse){
  return {
    link: function(scope,elm,attrs) {
      var clickHandler = function(){
        scope.$apply(function(){
           $parse(attrs.ngIf).assign(scope.$parent,'')
        })
        $document.off('click', clickHandler);
      };

      $document.on('click', clickHandler)
    }
  }
});

html:

<div ng-if="errorMessage" error-box class="alert error">
  <div class="alert-text">{{ errorMessage }}</div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can add a $scope variable that keeps track if it's 'allowed' to click. Then you can do something like this

<div ng-click="isDisabled || yourAction()"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.