-2

我有一个div点击我调用方法。 现在,有一个“取消”按钮,点击它我将$ scope.variable设置为true。

接下来,我只需在$ scope.variable设置为false的情况下点击'div'就可以执行我的函数。

但它现在工作!你能帮我解决这个问题吗?

这里是我的代码:

angular.module('app', ['ngSanitize']).controller('Ctrl', function($scope) { 
 
$scope.stopFunc = function() { 
 
    $scope.stopFuncExec = true; 
 
} 
 
$scope.stopFuncExec == false; 
 
$scope.myFunc1 = function() { 
 
    console.log("Inside " + $scope.stopFuncExec); 
 
    var whoAreYou = "Coder"; 
 
    if (whoAreYou == "Coder" && $scope.stopFuncExec == false) { 
 
    console.log("Hello, stop me if you can!"); 
 
    } 
 
} 
 
});
.parent { 
 
    height: 100px; 
 
    width: 200px; 
 
    background: skyblue; 
 
    border: 1px solid lightgrey; 
 
    border-radius: 5px; 
 
    padding: 20px; 
 
    margin: 20px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="Ctrl"> 
 
    <div class="parent"> 
 
    <div ng-click="myFunc1()">Click Me!</div> 
 
    </div> 
 
    <button ng-click="stopFunc()">Cancel</button> 
 
</div>

+0

你可能已经编辑[这个问题](https://stackoverflow.com/q/46745248/2435473) –

+0

是的,我很抱歉。但是,那么在这个问题上改变我自己的错误代码是不正确的。因此,我在这里添加了它,你能帮忙吗? – Sunny

回答

2

你错字:$scope.stopFuncExec == false;

你的意思是:$scope.stopFuncExec = false;


$scope.stopFuncExec == false;$scope.stopFuncExecundefined

Fixed Demo

+0

是的,我多么愚蠢!非常感谢! – Sunny

1

正如你所说的,功能应该被调用,只有当变量是假的。我相信你可以按照下面的方法解决这个问题。该功能将在点击Div时被调用,但您可以在下面检查条件。

如果变量为false,那么只有它会执行该块。我希望这会解决你的问题。

$scope.myFunc1 = function() 
{ 
    if(!$scope.stopFuncExec) 
    { 
     console.log("Inside " + $scope.stopFuncExec); 
     var whoAreYou = "Coder"; 
     if (whoAreYou == "Coder" && $scope.stopFuncExec == false) 
     { 
      console.log("Hello, stop me if you can!"); 
     } 
    } 
} 
相关问题