Dynamic Form Fields using ng-repeat Directive

In this approach, we create the dynamic and attractive form using the ng-repeat directive in AngularJS. Here we have used the array of objects that will store the information about the users. Each user can add his information along with the Gender and Interest from the radio and check buttons. Here, the ng-repeat directive dynamically generates the input fields, radio buttons, and checkboxes for each item in the array. This approach and easily be used for the dynamic creation and manipulation of the form fields.

Example: Below is an example that demonstrates the creation of dynamic forms in AngularJS using ng-repeat Directive.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.min.js">
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
  
        .container {
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 10px;
            max-width: 400px;
            width: 100%;
        }
  
        h1 {
            color: green;
        }
  
        h3 {
            font-weight: bold;
            margin-top: 10px;
        }
  
        form {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
  
        .add-button,
        .remove-button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px;
            cursor: pointer;
            border-radius: 5px;
            text-align: center;
            transition: background-color 0.3s;
        }
  
        .remove-button {
            background-color: #FF0000;
        }
  
        .add-button:hover {
            background-color: #45a049;
        }
  
        .remove-button:hover {
            background-color: #CC0000;
        }
  
        .form-element {
            display: flex;
            flex-direction: column;
        }
  
        .form-label {
            margin-bottom: 5px;
        }
  
        .error-message {
            color: red;
        }
  
        @media screen and (max-width: 600px) {
            .container {
                padding: 10px;
            }
  
            form {
                gap: 5px;
            }
  
            .form-element {
                gap: 5px;
            }
        }
  
        .user-details-container {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
  
        .user-details {
            flex: 1;
            background-color: #f0f0f0;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            display: flex;
            flex-direction: column;
        }
  
        .user-details h4 {
            font-weight: bold;
        }
  
        .user-details p {
            margin: 5px 0;
        }
  
        .form-element input[type="text"] {
            margin: 5px 0;
        }
  
        .form-element input[type="checkbox"] {
            margin: 0 5px;
        }
  
        .radio-container {
            display: flex;
            gap: 20px;
            margin: 5px 0;
        }
  
        .radio-container label {
            margin-right: 5px;
        }
    </style>
</head>
  
<body ng-app="DynamicFormsApp" 
      ng-controller="FormController">
    <div class="container">
        <h1 style="color: #4CAF50;">
              w3wiki
          </h1>
        <h3>
              Approach 1: Dynamic Form Fields Using ng-repeat
          </h3>
        <form name="dynamicForm">
            <div ng-repeat="name in names track by $index" 
                 class="form-element" 
                 ng-show="$index === names.length - 1">
                <input type="text" 
                       ng-model="name.name"
                       name="name{{$index}}" 
                       placeholder="Name {{$index + 1}}">
                <div class="form-element">
                    <label class="form-label">
                          Select a Gender for {{name.name}}:
                      </label>
                    <label>
                        <input type="radio"
                               ng-model="name.gender" 
                               value="male"
                               id="male{{$index}"> Male
                    </label>
                    <label>
                        <input type="radio"
                               ng-model="name.gender"
                               value="female"
                               id="female{{$index}"> Female
                    </label>
                </div>
                <div class="form-element">
                    <label class="form-label">
                          Select Interests for {{name.name}}:
                      </label>
                    <label>
                        <input type="checkbox"
                               ng-model="name.interests.sports"
                               id="sports{{$index}"> Sports
                    </label>
                    <label>
                        <input type="checkbox" 
                               ng-model="name.interests.movies"
                               id="movies{{$index}"> Movies
                    </label>
                    <label>
                        <input type="checkbox" 
                               ng-model="name.interests.travel"
                               id="travel{{$index}"> Travel
                    </label>
                </div>
                <button ng-click="removeName($index)"
                        class="remove-button">
                      Remove
                  </button>
            </div>
            <button ng-click="addName()"
                    class="add-button">
                  Add Name
              </button>
            <p class="error-message"
               ng-show="dynamicForm.$invalid">
               Please fill out all required fields.
            </p>
        </form>
        <div class="user-details-container">
            <div ng-repeat="name in names track by $index" 
                 class="user-details" 
                 ng-show="name.name">
                <h4>User {{$index + 1}}</h4>
                <p>
                      <strong>Name:</strong> {{name.name}}
                  </p>
                <p>
                      <strong>Gender:</strong> {{name.gender}}
                  </p>
                <p>
                      <strong>Interests:</strong>
                  </p>
                <ul>
                    <li ng-show="name.interests.sports">
                          Sports
                      </li>
                    <li ng-show="name.interests.movies">
                          Movies
                      </li>
                    <li ng-show="name.interests.travel">
                          Travel
                      </li>
                </ul>
            </div>
        </div>
    </div>
  
    <script>
        angular.module('DynamicFormsApp', [])
            .controller('FormController', function ($scope) {
                $scope.names = [{
                    name: '', gender: '',
                    interests: { sports: false, 
                                 movies: false, 
                                 travel: false }
                }];
  
                $scope.addName = function () {
                    $scope.names.push({
                        name: '', gender: '',
                        interests: { sports: false,
                                     movies: false, 
                                     travel: false }
                    });
                };
                $scope.removeName = function (index) {
                    $scope.names.splice(index, 1);
                };
            });
    </script>
</body>
  
</html>


Output:

How to create the dynamic forms in AngularJS ?

In AngularJS, we need to create dynamic forms to make the user interact. We can create and apply different behaviors to the forms to represent and accept the data from the user. We can create dynamic forms using ng-repeat and ng-show in AngularJS. In this article, we will see how we can create dynamic forms in AngularJS.

Similar Reads

Steps to create dynamic forms in AngularJS

The below steps will be followed for creating dynamic forms in AngularJS Applications....

Dynamic Form Fields using ng-repeat Directive

In this approach, we create the dynamic and attractive form using the ng-repeat directive in AngularJS. Here we have used the array of objects that will store the information about the users. Each user can add his information along with the Gender and Interest from the radio and check buttons. Here, the ng-repeat directive dynamically generates the input fields, radio buttons, and checkboxes for each item in the array. This approach and easily be used for the dynamic creation and manipulation of the form fields....

Dynamic Form Fields using ng-show Directive

...