你如何使用 angular javascript 获得表单的价值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31955696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:38:34  来源:igfitidea点击:

How do you get value of form using angular javascript?

javascriptangularjsangularjs-scope

提问by Lemon

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.

一直试图在表单提交上使用 angular js 来获取表单的值。有人帮我做什么。我是 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){

)}

回答by Arun P Johny

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

注意:要使提交处理程序起作用,您需要一个表单和一个提交按钮,如上所示