How to assign and read values of two different submit buttons using AngularJS?
Last Updated :
31 Jul, 2024
In this article, we will see how to assign and read the two different submit buttons' values in AngularJS. Usually, many websites provide relevant kinds of information and also supportive submit buttons like one or more than one also to proceed further.
For instance,
- Users will be asked to fill out the form and there are options in the form of submit buttons like “submit the form”, “Clear out the fields” or even “Links to similar pages” etc.
- On the main opening page of any website, we can see “Register with Email“, “Login”, “Login via Google”, “Login via Facebook” etc.,
Each submission needs to be handled in such a way that they need to do their respective action. In this article, let us see how we can do that via AngularJS by our following examples.
Explanation of ng controls:
- ng-app: Our root element and it is named a simple app.
- ng-controller: Defines the application controller. Here “MainCtrl” is a JavaScript function that is going to help out to handle multiple submit buttons the way we like.
- We define a controller with a variable exposed on its instance called username as well as password.
- We pointed the value for the ng-model at the same username variable on the MainCtrl.
- Hence any changes in the input box will update the model in our controller.
- When the value of the variable changes in the controller, the input field gets the value updated automatically.
- We can clearly see that code is not using any id or CSS, because of this ng-model, values are getting updated automatically
- ng-click-> On button click actions need to be performed.
- Two separate functions are written, one for clearing values and another for submitting. Hence, clean separation is happening via these different functions. Like this "n" number of functions can be written and divert the functionality according to the requirements.
The advantage is the controller has never reached out in UI, no JQuery selector, no findElementById, etc involved. On updating model fields in the controller, UI is updated and also latest values can be grabbed in a nice AngularJS way.
Example 1: This example describes the assign and reads values of two different submit buttons in AngularJS.
HTML
<!DOCTYPE html>
<html ng-app="simpleapp">
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
</head>
<body style="text-align: center;"
ng-controller="MainCtrl as mainctrl">
<h1 style="color: green;">GeeksforGeeks</h1>
<h3>
Assign and read values of two different
submit buttons using AngularJS
</h3><br>
UserName :
<input type="text" ng-model="mainctrl.username">
<br><br>
Password :
<input type="password" ng-model="mainctrl.password">
<br><br>
<button ng-click="mainctrl.clear()">
Clear Values
</button>
<button ng-click="mainctrl.submit()">
Submit
</button>
<script type="text/javascript">
angular.module('simpleapp', [])
.controller('MainCtrl', [function () {
var self = this;
self.clear = function () {
// Clearing fields, given
// as empty values
self.username = '';
self.password = '';
};
self.submit = function () {
console.log('YOUR INFORMATION: ',
self.username, self.password);
};
}]);
</script>
</body>
</html>
Output: On click of "Clear Values" & On click of the "Submit" button, the output is seen:
Example 2: In the below example, multiple submits happen via two different forms and two submit buttons. i.e. “ng-submit” is used instead of button controls.
In the first form, the username and password are directly on the controller whereas, for the second form, they are bound to a user object in the controller. Hence, while submitting the form, the output code differs a little. i.e. for the second form, we can directly access as a self.user because, For the second form, our ng-model is mainctrl.user.username and mainctrl.user.password.
So, during form2 submission, we can get the value as self.user. In both ways, we can prefer. But if we are bound to an object, all the contents of the form are accessed via that object easily.
HTML
<!DOCTYPE html>
<html ng-app="multiformapp">
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js">
</script>
</head>
<body style="text-align: center;"
ng-controller="MainCtrl as mainctrl">
<h1 style="color: green;">GeeksforGeeks</h1>
<h3>
Assign and read values of two different
submit buttons using AngularJS
</h3>
<form ng-submit="mainctrl.form1submit()">
<input type="text" ng-model="mainctrl.username">
<input type="password" ng-model="mainctrl.password">
<input type="submit" value="Form 1 Submit">
</form>
<form ng-submit="mainctrl.form2submit()">
<input type="text" ng-model="mainctrl.user.username">
<input type="password" ng-model="mainctrl.user.password">
<input type="submit" value="Form 2 Submit">
</form>
<script type="text/javascript">
angular.module('multiformapp', [])
.controller('MainCtrl', [function () {
var self = this;
self.form1submit = function () {
// Create user object to send
// to the server
var user = {
username: self.username,
password: self.password
};
console.log('First form information ',
self.username, self.password);
};
self.form2submit = function () {
console.log('Second form information ',
self.user);
};
}]);
</script>
</body>
</html>
Output:
While verifying the output of both forms submits, Form 2 Submit provides an easier way of Accessing model objects.
Conclusion: AngularJS is very user-friendly and as it provides an MVC framework, a clean neat user interface in HTML, two-way binding, etc., Depending upon the requirements, we can handle events for different submit buttons easily and divert the flow accordingly.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read