The ng-include directive can be the perfect choice to dynamically add pieces of HTML code. Per example, the below code will allow us to display some data in a tabular structure or in a list. We can switch between list and table with a simple checkbox.
The data displayed in a table:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Rank</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in atp">
<td>{{item.name}}</td>
<td>{{item.rank}}</td>
</tr>
</tbody>
</table>
Same data displayed in a list:
<ul>
<li ng-repeat="item in atp">
{{item.rank}}.{{item.name}}
</li>
</ul>
Main page that allows us to switch between table and list:
<!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" />
<script>
angular.module("atpModule", [])
.controller("atpCtrl", function ($scope) {
$scope.includePieceOfHTML = function () {
return $scope.showWhat ? "list.html" : "table.html";
};
});
</script>
</head>
<body>
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
{ name: 'Nadal, Rafael (ESP)', rank: 1 },
{ name: 'Djokovic, Novak (SRB)', rank: 2 },
{ name: 'Federer, Roger (SUI)', rank: 3 },
{ name: 'Wawrinka, Stan (SUI)', rank: 4 },
{ name: 'Ferrer, David (ESP)', rank: 5 }]">
<div id="atpPanel" class="panel">
<h3 class="panel-header">ATP SINGLES RANKINGS</h3>
<div class="well">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="showWhat">
Show the list
</label>
</div>
</div>
<ng-include src="includePieceOfHTML()"></ng-include>
</div>
</div>
</body>
</html>
Niciun comentariu:
Trimiteți un comentariu