Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get value of form using angular javascript?

been trying to get the value of form using angular js on form submit. someone help me what to do. I am new to angular JS.

<div id="content" ng-app="TryApp" ng-controller="AppController" ng- submit="submit()">  

<input type="text" placeholder="first name" name='name' ng-  model="user.Name"/>
<br>
<input type="text" placeholder="email address" name="name" ng-model="user.email"/>
<br>
<input type="text" placeholder="age" name="age" ng-model="user.age"/>
<br>

script.

App.controller('AppController', function ($scope){

)}
like image 418
Lemon Avatar asked Aug 12 '15 03:08

Lemon


People also ask

What is form data in angular?

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the fetch() or XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data" .

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

What is form in AngularJS?

Forms in AngularJS provides data-binding and validation of input controls.

What is the use of Ngsubmit in angular?

The ng-submit directive specifies a function to run when the form is submitted. If the form does not have an action ng-submit will prevent the form from being submitted.


1 Answers

var app = angular.module('TryApp', [], function() {})

app.controller('AppController', function($scope) {
  $scope.user = {};
  $scope.submit = function() {
    //here $scope.user will have all the 3 properties
    alert(JSON.stringify($scope.user));
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="content" ng-app="TryApp" ng-controller="AppController">
  <form ng-submit="submit()">
    <input type="text" placeholder="first name" name='name' ng-model="user.Name" />
    <br>
    <input type="text" placeholder="email address" name="name" ng-model="user.email" />
    <br>
    <input type="text" placeholder="age" name="age" ng-model="user.age" />
    <br>
    <input type="submit" value="Save" />
    <p>{{user | json }}</p>
  </form>
</div>

Note: for submit handler to work, you need a form and a submit button as shown above

like image 77
Arun P Johny Avatar answered Sep 27 '22 00:09

Arun P Johny