Angular JS Magazine

miercuri, 3 septembrie 2014

Working with Routes and Views

This tip consist in a simple example of using views and routes. Do not forget to add the ngRoute module:

Main code:

<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <script src="../angular/angular-route.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
    <script>
        var app = angular.module("atpModule", ['ngRoute'])
                .config(function ($routeProvider) {
                    $routeProvider
                            .when('/', {
                                templateUrl: './views/view1.html',
                                controller: 'atpCtrl1'
                            })
                            .when('/view2', {
                                templateUrl: './views/view2.html',
                                controller: 'atpCtrl2'
                            })
                            .otherwise({redirectTo: '/'});
                })
                .controller("atpCtrl1", function ($scope) {
                    // Will be printed in 'view1'
                    $scope.atpPlayers = [
                        { 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 }
                    ];
                })
                .controller("atpCtrl2", function ($scope) {

                    // Will be printed in 'view2'
                    $scope.atpPlayers = [
                        { name: 'Raonic, Milos (CAN)', rank: 6 },
                        { name: 'Berdych, Tomas (CZE)', rank: 7 },
                        { name: 'Dimitrov, Grigor (BUL)', rank: 8 },
                        { name: 'Murray, Andy (GBR)', rank: 9 },
                        { name: 'Tsonga, Jo-Wilfried (FRA)', rank: 10 }
                    ];
                });
    </script>
</head>
<body>
<h2>Routes and Views</h2>
<a href="#/view1" title="Open 'view1'">Open 'view1'</a>
<a href="#/view2" title="Open 'view2'">Open 'view2'</a>

<!-- AngularJS views will be loaded here on page navigation -->
<div ng-app="atpModule" ng-view=""></div>

</div>
</body>
</html>

View 1 code:

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

View 2 code (almost identical):

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

View 1:

View 2: