Search code examples
angularjsmethodsangularjs-scopeconditional-statementsangularjs-ng-click

Call a method only if my $scope variable is false


I've a div on click of which I'm calling a method. Now, there's a 'Cancel' button, on click of which I'm setting a $scope.variable to true.

Next, I need to execute my function on click of the 'div', only if $scope.variable is set to false.

But it is now working! Could you help me fix this?

Here's my code:

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>


Solution

  • You have typo: $scope.stopFuncExec == false;

    Did you mean: $scope.stopFuncExec = false;?


    After $scope.stopFuncExec == false; the $scope.stopFuncExec will be undefined

    Fixed Demo