Angular:从js添加ngClick [英] Angular: Add ngClick from js
本文介绍了Angular:从js添加ngClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Angular开发用于移动设备的小型网页。
我想切换整个页面的点击侦听器,例如
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',...);
但以Angular方式。而且我不想在某些情况下使用 ng-Click
,因为在大多数情况下,点击都不起作用,这只是为了处理错误。有可能还是我需要使用纯js / jQuery?
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?
面板HTML:
<div class="alert error" ng-show="errorMessage">
<div class="alert-text">{{ errorMessage }}</div>
</div>
推荐答案
我创建了一条指令:
- 应与
ngIf
结合使用。 -
ngIf
应该引用范围内的可分配变量 - 例如,不要这样做:
ng-if ='myFunc()'
或ng-if ='var1 || var2'
- 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()'
orng-if='var1 || var2'
I created a directive:
这是一个朋克: http://plnkr.co/edit/fmNH2PbRrruRqGFYiui1?p=preview
指令:
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>
这篇关于Angular:从js添加ngClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文