Angular JS Magazine

duminică, 31 august 2014

Angular and Form Elements - part 3

In Angular and Form Elements - part 2 we have introduced a form with Angular validation capabilities. Further, we have modified this form to support CSS notifications for valid and invalid data. In order to accomplish this task, we need to specify the classes used by AngularJS Validation - actually we just need to shape the CSS code because Angular will automatically add the form elements to the right class depending on their validity status:

ng-pristine = elements that the user has not interacted are added to this class
ng-dirty = elements that the user has interacted are added to this class
ng-valid = elements that are valid are added in this class
ng-invalid = elements that are invalid are added in this class

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope) {
                    $scope.addNewPlayer = function (newPlayer) {
                        if (angular.isDefined(newPlayer)) {
                            $scope.atp.push({name:, email:,
                                                        rank: $scope.atp.length + 1});
        form { background-color: coral; }
        form { background-color: lightcyan; }
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', email: '', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', email: '', rank: 2 },
     { name: 'Federer, Roger (SUI)', email: '', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', email: '', rank: 4 },
     { name: 'Ferrer, David (ESP)', email: '', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <tr ng-repeat="item in atp">
    <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="" required>
                <div class="form-group row">
                    <label for="playerEmailId">Player E-mail:</label>
                    <input id="playerEmailId" type="email" class="form-control"
                              ng-model="" required>
                <div class="form-group row">
                    <label for="playerRankId">Player Rank:</label>
                    <span id="playerRankId">{{atp.length + 1}}</span>
                <button class="btn btn-primary btn-block" ng-disabled="atpForm.$invalid">
                    Add Player

Form contains invalid fields:

Form contains only valid fields: