Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-click does not work for second time with ng-hide interaction

I have two buttons and only 1 of them appears at a time. First one is called "Offline mode" other one is "Online mode". They appear like this on HTML:

<a ng-hide="offlineMode == true" ng-click="offlineMode()">Offline Mode</a> 
<a ng-show="offlineMode == true" ng-click="onlineMode()">Online Mode</a>

When I click to "Offline mode" for the first time it successfuly swaps into "Online mode" button. When I click "Online mode" after that it swaps back to "Offline mode". After this the button becomes unclickable. It does not enter inside the function.

JS:

$scope.offlineMode = function() {
    $scope.offlineMode = true;
    // do other things
};

$scope.onlineMode = function() {
    $scope.offlineMode = false;
    // do other things
};
like image 208
brainmassage Avatar asked Jan 28 '26 06:01

brainmassage


1 Answers

You are getting this error because both the function and flag variable have the same name offlineMode

See console for this error:

v2.offlineMode is not a function

Fix: Simply rename the boolean variable to offline(or something else)

  $scope.offlineMode = function() {
      $scope.offline = true;
      // do other things
  };

  $scope.onlineMode = function() {
      $scope.offline = false;
      // do other things
  };  

ANd update the html accordingly

<a href="#" ng-hide="offline" ng-click="offlineMode()">Offline Mode</a><br> 
<a href="#" ng-show="offline" ng-click="onlineMode()">Online Mode</a>

Notice the changes in ng-hide and ng-show. You dont need to evaluate offline with true each time.

Here's the demo

like image 172
nalinc Avatar answered Jan 29 '26 19:01

nalinc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!