2

I'm making a small example to change the value of ng-hide after clicking button, but it didn't work.

I've tried:

angular.module('myModule', []).controller('myController', function ($scope) {
  $scope.hide= false
  $scope.change = function () {
    $scope.hide = !$scope.hide  
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myModule" ng-controller="myController">
  <p ng-hide="{{hide}}">some text...</p>
  <button ng-click="change()">change</button>
</div>

And my question: what am I missing?

Tân
  • 1
  • 15
  • 56
  • 102

3 Answers3

4

Remove the brackets around hide in your template.. that will help.

ng-hide="hide"
Keugels
  • 790
  • 5
  • 15
1

You can use a ternary condition like

$scope.hide = ($scope.hide) ? false : true;
albttx
  • 3,444
  • 4
  • 23
  • 42
1

Angular uses {{...}} to mark something as an expression. However hide is already an angular expression so you get expression of expression which breaks the two way binding and is only evaluated once on pageloading...

So to fix your problem simply remove the brackets

ng-hide="hide"

See here for more information.

angular.module('myModule', []).controller('myController', function ($scope) {
  $scope.hide= false
  $scope.change = function () {
    $scope.hide = !$scope.hide
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myModule" ng-controller="myController">
  <p ng-hide="hide">some text...</p>
  <button ng-click="change()">change</button>
</div>
Community
  • 1
  • 1
Fabian N.
  • 3,807
  • 2
  • 23
  • 46