AngularJS
AngularJS Directive with Example: ng-init, ng-repeat, ng-app, ng-model
What is an AngularJS directive? A directive in AngularJS is a command that gives HTML new...
The processes of submitting information on a web page are normally handled by the submit event on the web browser. This event is normally used to send information which the user might have entered on a web page to the server for further processing like login credentials, form data, etc. The submission of information can be done through GET or POST request.
AngularJS also provides a directive called ng-submit which can be used to bind the application to the submit event of the browser. So in the case of AngularJS, on the submit event you can carry out some processing within the controller itself and then display the processed information to the user.
Let's take an example of how we can achieve this.
In our submit-post example,
We are going to present a textbox to the user in which they can enter the topic which they want to learn. There will be a submit button on the page, which when pressed will add the topic to an unordered list.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body ng-app="sampleApp">
<script src="https://code.angularjs.org/1.6.9/angular-route.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="/lib/bootstrap.js"></script>
<script src="/lib/bootstrap.css"></script>
<h1> gtupapers Global Event</h1>
<div ng-controller="AngularController">
<form ng-submit="Display()">
Enter which topic you would like to learn
<input type="text" ng-app="sampleApp" ng-model="Topic"><br>
<input type="submit" value="Submit"/>
<ul ng-repeat="topicname in AllTopic">
<li>{{topicname}}</li>
</ul>
</form>
</div>
<script>
var sampleApp = angular.module("sampleApp",[]);
sampleApp.controller("AngularController",function($scope) {
$scope.AllTopic=[];
$scope.Display = function () {
$scope.AllTopic.push($scope.Topic);
}
});
</script>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
To see the code working, first, enter a Topic name like "AngularJS" as shown above in the textbox and then click on the Submit button.
Summary
The "ng-submit" directive is used to process the input entered by the user when the form is submitted.
What is an AngularJS directive? A directive in AngularJS is a command that gives HTML new...
We have prepared the most frequently asked Angular interview questions and answers that acquaint...
What is Protractor Testing? PROTRACTOR is an automation and end-to-end behavior-driven testing tool...
What is ng-model in AngularJs? ng-model is a directive in Angular.JS that represents models and its...
What is React JS? React is a Javascript library developed by Facebook which allows you to build UI...
What is $scope in AngularJS? $scope in AngularJS is a built-in object which basically binds the...