Angular: Add ngClick from js

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>

Answers


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>

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>

Need Your Help

numberOfRowsInSection not being called for UITableView

iphone objective-c ios5 xcode4.2

I am having some issues where my UITableView is not reloading, I have redone the linking of the outlet to make sure that was not the issue. I have also tried using [self table] reloadData] but that...

create databases and tables dynamically with Yii framework possible?

yii

I am developing a system with a master database and multiple databases, once for each client. The client database and its tables will be created when the client fills in and submit a form with all ...