it-swarm.com.de

Wie kann man ein Div ein- und ausblenden, basierend auf der Modellbedingung in Angularjs?

Ich möchte ein Div basierend auf der Checkbox ein-/ausblenden. Scheint ziemlich einfach. Ich speichere den Wert der Checkbox in einem Modell und verwende ihn in div ng-show. Was mache ich falsch?

<div ng-app='visibleApp'>
    <div ng-controller='myController'>
         <input type="checkbox" name="hideBasicInfo" ng-model="hideBasicInfo">hide the basic information section
         <div ng-show="{{!hideBasicInfo}}">
             <label for="firstName">First Name:</label>
             <input type="text" name="firstName" ng-model="firstName"/></br>

             <label for="middleName">Middle Name:</label>
             <input type="text" name="middleName" ng-model="middleName"/></br>
             <label for="lastName">Last Name:</label>
             <input type="text" name="lastName" ng-model="lastName"/>
         </div>
         <hr/>
         <div>
             <h4>Debug Information</h4>
             hideBasicInfo: {{hideBasicInfo}}<br/>
             !hideBasicInfo: {{!hideBasicInfo}}
         </div>
     </div>
</div>

JS-Datei:

var visibleApp = angular.module('visibleApp',[]);

visibleApp.controller('myController', function($scope){
     $scope.data = "my data";
     $scope.hideBasicInfo = false; 
});

Vielen Dank.

Siehe Geige

10
U-L

fast dort...

 <div ng-hide="hideBasicInfo">
    ...
 </div>

es werden keine Schablonenklammern ({{}}) benötigt.

13
ben schwartz
<div ng-show='One'>
 <p>Section One</p>
</div>

<div ng-show='Two'>
 <p>Section Two</p>
</div>

<div ng-show='Three'>
 <p>Section Three</p>
</div>

<!-- Navigation -->
<nav>
 <a href='#' ng-click='showOne'> Show Div One </a>
 <a href='#' ng-click='showTwo'> Show Div Two </a>
 <a href='#' ng-click='showThree'> Show Div Three </a>
</nav>
0
Saravanan R
Execute this code:`enter code here`
<!DOCTYPE html>
<html>
<head>
  <script src="angular.js"></script>
</head>

<body ng-app>
  <h3>1. Show</h3>
  <label>Show the square: <input type="checkbox" ng-model="mustShow" /></label><br />
  <div ng-show="mustShow" style="width: 50px; height: 50px; background-color: red;"></div><br />
  <br />
  <h3>2. Hide</h3>
  <label>Hide the square: <input type="checkbox" ng-model="mustHide" /></label><br />
  <div ng-hide="mustHide" style="width: 50px; height: 50px; background-color: green;"></div>
</body>
</html>
0
Saravanan R