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

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

问题描述

我点击了一个 div,我正在调用它的一个函数.

<!--这里有一些数据...-->

<button ng-click="stopmyFunc1===true">取消</button>

在控制器中:

$scope.myFunc1 = function(){if(myStr == 'abc' && stopmyFunc1 === false){//这里有一些代码...}};

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

我该如何解决这个问题?

解决方案

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

<div ng-click="stopmyFunc1 === undefined && myFunc1()"><!--这里有一些数据...-->

<button ng-click="stopmyFunc1 = true">取消</button>

这样您就不需要在控制器中进行额外检查...

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>

In controller :

$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.

How could I fix this?

解决方案

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天全站免登陆