Angular JS Magazine

miercuri, 27 august 2014

Show/Hide Elements

The ng-show and ng-hide directives control element visibility. The ng-show with value true will show the element, while with value false will hide the element. The ng-hide is viceversa of ng-show.

Per example, ng-show can be used to hide some records in a table:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
</head>
<body>
<div ng-app="" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1, available: true },
     { name: 'Djokovic, Novak (SRB)', rank: 2, available: true },
     { name: 'Federer, Roger (SUI)', rank: 3, available: false },
     { name: 'Wawrinka, Stan (SUI)', rank: 4, available: true },
     { name: 'Ferrer, David (ESP)', rank: 5, available: false }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <thead>
            <tr>
                <th>Name</th>
                <th>Rank</th>
            </tr>
            </thead>
            <tbody>
             <tr ng-repeat="item in atp" ng-show="item.available">
                 <td>{{item.name}}</td>
                 <td>{{item.rank}}</td>
              </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>


Now, you can easily test ng-hide.

Note: These two directives affects some behind the scene CSS, therefore they DON'T remove the element from the DOM, just hide it.