Angular JS Magazine

duminică, 31 august 2014

Angular and Form Elements - part 2

In this tip we extend the Angular and Form Elements - part 1 with validation:

<!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.addNewPlayer = function (newPlayer) {
                        if (angular.isDefined(newPlayer)) {
                            $scope.atp.push({name: newPlayer.name, rank: $scope.atp.length + 1});
                        }
                    };
                });
    </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>
        <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>
    </div>
    <div class="col-xs-6">
        <form name="atpForm" novalidate ng-submit="addNewPlayer(player)">
            <div class="well">
                <div class="form-group row">
                    <label for="playerNameId">Player Name:</label>
                    <input id="playerNameId" type="text" class="form-control"
                              ng-model="player.name" required>
                </div>
                <div class="form-group row">
                    <label for="playerRankId">Player Rank:</label>
                    <span id="playerRankId">{{atp.length + 1}}</span>
                </div>
                <button class="btn btn-primary btn-block" ng-disabled="atpForm.$invalid">
                    Add Player
                </button>
            </div>
        </form>
    </div>
</div>
</body>
</html>




There are several things to notice here, as follows:

1. name="atpForm" - among other things, the name attribute is useful to access the validity status via $valid and $invalid variables. There are also: $pristine (returns true if the user has not interacted with the element/form), $dirty (returns true if the user has interacted with the element/form) and $error (details of validation errors).
2. novalidate - this directive disables the browser validation support and enables the Angular features
3. ng-submit="addNewPlayer(player)" - this directive indicates a custom response to a submit event (triggered when the user submits the form)
4. type="text" - this attribute specifies the data type that the input element will gather. It could be: email, number, text, radio, url, checkbox.
5. required - indicates that the user is required to enter a value.
6. ng-disabled="atpForm.$invalid" - while the form is not valid the submit button is disabled. You can check validity using $valid also.

Niciun comentariu:

Trimiteți un comentariu