仅当变量设置为true时才调用函数 [英] Call a function only if a variable is set to true

查看:97
本文介绍了仅当变量设置为true时才调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击一个div时,我正在调用该函数.

I've a div on click of which I'm calling a function.

<div ng-click="myFunc1()">
    <!--some data here...-->
</div>

<button ng-click="stopmyFunc1===true">Cancel</button>

在控制器中:

$scope.myFunc1 = function(){
  if(myStr == 'abc' && stopmyFunc1 === false){
    //some code here...
  }
};

我需要在单击取消"按钮后停止执行我的功能. 但是,当前使用我编写的代码,似乎没有任何效果.

I need to stop executing my function on click of "cancel' button. But, currently with the code I've written, nothing seems to work.

我该如何解决?

推荐答案

您可以像这样直接在点击处理程序中检查停止标记":

You can check the 'stop flag' directly in your click handler like this:

<div ng-click="stopmyFunc1 === undefined && myFunc1()">
    <!--some data here...-->
</div>

<button ng-click="stopmyFunc1 = true">Cancel</button>

因此,您无需在控制器中进行额外检查...

With that you don't need an additional check in your controller...

这篇关于仅当变量设置为true时才调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆