The Wayback Machine - https://web.archive.org/web/20240704181700/https://www.geeksforgeeks.org/how-to-get-form-input-value-using-angularjs/
Open In App

How to get form input value using AngularJS ?

Last Updated : 01 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given a form element and the task is to get the form values input by the user with the help of AngularJS.

Approach: 

  • We are storing the data in the JSON object after the user enters it in the input element with the help of the Angular.copy(object) method. 
  • After that, data from the JSON object can be accessed easily.

Example 1: In this example, the data is stored in the JSON object and accessed from it.

HTML




<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.userData = '';
            $scope.getData = function (user) {
                $scope.userData = angular.copy(user);
            };
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
  
    <p>
        How to get form input value in AngularJS
    </p>
  
    <div ng-app="app">
        <div ng-controller="controller">
            <form action="javascript:void(0)">
                firstName: <input type="text" 
                    ng-model="user.fName" /><br>
                lastName : <input type="text" 
                    ng-model="user.lName" /><br>
                <br>
                <button ng-click="getData(user)">
                    SAVE
                </button>
            </form>
  
            <p>FirstName = {{userData.fName}}</p>
  
  
            <p>lastName = {{userData.lName}}</p>
  
        </div>
    </div>
</body>
  
</html>


Output:

Example 2: In this example, the data is stored in the JSON object and the JSON object’s data is printed. 

HTML




<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller", function ($scope) {
            $scope.userData = '';
            $scope.getData = function (user) {
                $scope.userData = angular.copy(user);
            };
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
  
    <p>
        How to get form input 
        value in AngularJS
    </p>
  
    <div ng-app="app">
        <div ng-controller="controller">
            <form action="javascript:void(0)">
                firstName: <input type="text" 
                    ng-model="user.fName" />
                <br>
                lastName : <input type="text" 
                    ng-model="user.lName" />
                <br>
                Ph.No.: <input type="text" 
                    ng-model="user.Phone" />
                <br>
                City: <input type="text" 
                    ng-model="user.city" />
                <br>
                <button ng-click="getData(user)">
                    SAVE
                </button>
            </form>
  
            <p>User Data Json = {{userData | json}}</p>
  
        </div>
    </div>
</body>
  
</html>


Output:



Similar Reads

How to get input value search box and enter it in AngularJS component using Enter key ?
In this article, we will see how to get an input value entered in the search box using Enter key in AngularJS. To implement a search component in AngularJS that calls the function whenever the user presses the enter key(keyCode = 13) and then does some relatable task from the user input. This can be achieved easily using the keyup event. Here for s
3 min read
How to Reset an Input Value when Limit Exceeds using AngularJS ?
The task is to handle an input field if the number entered by the user in the input exceeds some limit using angularJS. We define a limitTo directive and use it on an HTML input element. This directive is used for all the handling of overflowing limits. The directive calls a function on the keypress event where we can check for the limits. Example
2 min read
How to prevent form submission when input validation fails in AngularJS ?
Validation of fields is an important process while developing a dynamic web application. In terms of security, validation is an important concept. We pass the data from the client side through input fields to the server. If any of the fields is not validated properly and the user sends any irrelevant data then there can be cause of different securi
7 min read
How to get the value of radio button using AngularJS ?
In this article, we will see how to get the value of the radio button using AngularJS, along with understanding the basic implementation through illustrations. The value of the checked radio button can be fetched with the help of the ng-model directive that helps to bind the data with the specific element &amp; stores the required value in a variab
2 min read
How to set the input field value dynamically in AngularJS ?
Given an input field and the task is to set the input field value dynamically with the help of AngularJS. Approach: A value is being set to the input field despite the user enter something or not. ng-click event is used to call a function that sets the value to 'This is GeeksForGeeks' with the help of ng-model. Example 1: In this example, the input
2 min read
How to validate if input in input field exactly equals to some other value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type some exact value and based on that we give the user access to the request or deny the r
4 min read
How to validate if input in input field has boolean value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only boolean value i.e. true or false are allowed. We can also validate these input fields to accept onl
4 min read
Which input type is used to hide the input field to be displayed in HTML Form ?
The form tag is generally used to create forms for entering data. We can use different form tag attributes like text boxes, text area, checkboxes, radio buttons, buttons, dropdown list, etc. to collect data from the users. We need to use the HTML &lt;input type=”hidden”&gt; type for hiding the input field. Using this attribute, the input field will
1 min read
How to get the value in an input text box using jQuery ?
In this article, we will learn about the different methods to access the value of the input text box entered by the user using jQuery. Below are the different approaches listed to get the value of an input text box using jQuery: Table of Content Using the val() methodUsing the prop() methodLet us now discuss all the above-listed methods in detail w
3 min read
Get the value of text input field using JavaScript
To get the value of a text input field using JavaScript, you can use the DOM Input Text value Property of the input element. Syntax for getting the value of text inputField.value; Syntax for setting the value of text inputField.value = text; Using Value Property to get value: The value property in JavaScript represents the current value of an input
3 min read
three90RightbarBannerImg