Dynamic Form Fields using ng-show Directive

In this approach, we are using the ng-show directive to create the dynamic forms by selectively displaying the form steps which are based on the value of the currentStep variable. Here we have embedded each step in the div element container which is in the class of “form-step”. Using this approach, we create step-by-step dynamic forms that allow the users to enter the data for each step before going to the next step.

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

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
    </script>
    <link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body {
            background-color: #f0f8ff;
        }
  
        .form-container {
            width: 600px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #007BFF;
            background-color: #fff;
            font-family: Arial, sans-serif;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
  
        h1 {
            color: #008000;
        }
  
        .form-step {
            border: 1px solid #007BFF;
            margin-bottom: 20px;
            background-color: #f0f8ff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            display: none;
        }
  
        .active-step {
            display: block;
        }
  
        .form-button {
            background-color: #007BFF;
            color: #fff;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            font-size: 16px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
  
        .form-button:hover {
            background-color: #0056b3;
        }
  
        .form-button[disabled] {
            background-color: #0056b3;
            cursor: not-allowed;
        }
  
        .back-button,
        .forward-button {
            background-color: #0056b3;
            color: #fff;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            font-size: 16px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
  
        .back-button:hover,
        .forward-button:hover {
            background-color: #0056b3;
        }
  
        table {
            width: 100%;
            border: 1px solid #ddd;
            border-collapse: collapse;
        }
  
        th,
        td {
            padding: 10px;
            text-align: left;
            border: 1px solid #ddd;
        }
    </style>
</head>
  
<body ng-app="dynamicFormApp"
      ng-controller="DynamicFormController">
    <div class="form-container">
        <h1>w3wiki</h1>
        <h3>
              Approach 2: Dynamic Form Fields Using ng-show
          </h3>
        <form name="dynamicForm" ng-submit="submitForm()">
            <div class="form-step"
                 ng-class="{ 'active-step': currentStep === 1 }">
                <h3>Step 1: Personal Information</h3>
                <div>
                    <label for="firstName">First Name:</label>
                    <input type="text" 
                           id="firstName"
                           name="firstName"
                           ng-model="formData.firstName" required>
                </div>
                <div>
                    <label for="lastName">Last Name:</label>
                    <input type="text" 
                           id="lastName" 
                           name="lastName" 
                           ng-model="formData.lastName" required>
                </div>
                <button class="form-button" 
                        ng-click="nextStep(2)">Next
                  </button>
            </div>
  
            <div class="form-step" 
                 ng-class="{ 'active-step': currentStep === 2 }">
                <h3>Step 2: Contact Information</h3>
                <div>
                    <label for="email">Email:</label>
                    <input type="email" 
                           id="email"
                           name="email" 
                           ng-model="formData.email" required>
                </div>
                <div>
                    <label for="phone">Phone:</label>
                    <input type="tel" 
                           id="phone"
                           name="phone"
                           ng-model="formData.phone" required>
                </div>
                <button class="form-button back-button" 
                        ng-click="prevStep(1)">Back
                  </button>
                <button class="form-button forward-button"
                        ng-click="nextStep(3)">Next
                  </button>
            </div>
  
            <div class="form-step" 
                 ng-class="{ 'active-step': currentStep === 3 }">
                <h3>Step 3: Additional Information</h3>
                <div>
                    <label for="address">Address:</label>
                    <input type="text" 
                           id="address"
                           name="address" 
                           ng-model="formData.address" required>
                </div>
                <button class="form-button back-button" 
                        ng-click="prevStep(2)">Back
                  </button>
                <button class="form-button" 
                        ng-click="submitForm()" 
                        ng-disabled="currentStep !== 3">Submit
                  </button>
            </div>
        </form>
        <div ng-show="showSubmitted">
            <h3>Form Submitted!</h3>
            <table>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Address</th>
                </tr>
                <tr ng-repeat="user in userData">
                    <td>{{ user.firstName }}</td>
                    <td>{{ user.lastName }}</td>
                    <td>{{ user.email }}</td>
                    <td>{{ user.phone }}</td>
                    <td>{{ user.address }}</td>
                </tr>
            </table>
        </div>
    </div>
    
    <script>
        angular.module('dynamicFormApp', [])
            .controller('DynamicFormController', function ($scope) {
                $scope.currentStep = 1;
                $scope.showSubmitted = false;
                $scope.formData = {};
                $scope.userData = [];
                $scope.nextStep = function (step) {
                    $scope.currentStep = step;
                };
                $scope.prevStep = function (step) {
                    $scope.currentStep = step;
                };
                $scope.submitForm = function () {
                    $scope.userData.push(angular.copy($scope.formData));
                    $scope.formData = {};
                    $scope.currentStep = 1;
                    $scope.showSubmitted = true;
                };
            });
    </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

...