仅当我的$scope变量为 false 时才调用方法



我有一个div,我正在调用它的方法。现在,有一个"取消"按钮,单击该按钮时,我将$scope.变量设置为 true。

接下来,我需要在单击"div"时执行我的函数,仅当 $scope.variable 设置为 false 时。

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

这是我的代码:

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>

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

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


$scope.stopFuncExec == false;后,$scope.stopFuncExec将被undefined

固定演示

正如你所说,只有当变量为 false 时,才应该调用该函数。我相信您可以按照以下解决方法进行操作。该函数将在单击 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!");
        }
    }
}

相关内容

最新更新