Angular js. Pass data from form to model

Home » Tutorials » JavaScript » Angular js. Pass data from form to model
We will continue to build our angular app, Today we will realize adding data from form to model.
To make adding data to model from form with angular we need to do this steps:

  • adding angula directive ng-click to element which data will send to controller while click event. Value of this directive is method name. In our example it is addPlayer().
  • In controller we need to define method of the $scope object with the same name addPlayer. Also you need pass parameters to this method, namely form data. That’s why definition of the method will be addPlayer(playerName, playerTeam, playerCountry). Notice, parameters names match with ng-model directives in form input fields.
    As you know its purpose to bind data model with $scope object.

Code lesson

<!DOCTYPE html>
<html ng-app="playersApp">
    <head>
        <meta charset="utf-8">
        <title></title>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>

        <div class="container" ng-controller="playerCtrl">
            <div class="row">
                <div class="col-md-12">
                    <form class="form-horizontal">
                        <legend>Список футболистов</legend>
                          <div class="form-group">
                            <label for="playerName" class="col-sm-4 control-label">Имя игрока</label>
                            <div class="col-sm-8">
                              <input type="text" ng-model="playerName" class="form-control" id="playerName" placeholder="Имя игрока">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="playerTeam" class="col-sm-4 control-label">В какой команде играет</label>
                            <div class="col-sm-8">
                              <input type="text" ng-model="playerTeam" class="form-control" id="playerTeam" placeholder="В какой команде играет">
                            </div>
                          </div>
                          <div class="form-group">
                            <label for="playerCountry" class="col-sm-4 control-label">Гражданство</label>
                            <div class="col-sm-8">
                              <input type="text" ng-model="playerCountry" class="form-control" id="playerCountry" placeholder="Гражданство">
                            </div>
                          </div>
                          <div class="form-group">
                              <div class="col-sm-8 col-sm-offset-4">
                                  <button ng-click="addPlayer(playerName, playerTeam, playerCountry)" class="btn btn-default">Добавить</button>
                              </div>
                          </div>
                    </form>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Игрок</th>
                                <th>Команда</th>
                                <th>Национальность</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="player in players.playersObjects">
                                <td>{{player.name}}</td>
                                <td>{{player.team}}</td>
                                <td>{{player.country}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <script src="angular.js"></script>
        <script>
            var app = angular.module("playersApp", []);

            var model = {
                playersObjects: []
            }

            app.controller("playerCtrl", function($scope){
                $scope.players = model;
                $scope.addPlayer = function(playerName, playerTeam, playerCountry) {
                    $scope.players.playersObjects.push({name: playerName, team: playerTeam, country: playerCountry});
                }
            });
        </script>

    </body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This