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

时间:2015-08-12 03:50:11

标签: javascript angularjs angularjs-scope

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

脚本。

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

)}

1 个答案:

答案 0 :(得分:7)

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>

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