javascript - Angular - 在 Controller 中使用表单发布值

标签 javascript angularjs forms post scope

我正在用 AngularJS 编写我的第一个应用程序,而且我对 javascript 很陌生。我有一个简单的问题:

如何在 Angular Controller 中使用 html 表单中的 POST 值?

我有一个表单(为了便于阅读,我删除了 CSS 和验证内容):

<form name="signupForm" novalidate>

                <input type="text" placeholder="Name..." ng-model="name" name="name" required />

                <input type="email" name="email" ng-model="email" placeholder="Email..." required>

                <input type="password" placeholder="Password..." ng-model="password" name="password" required ng-minlength="7" ng-maxlength="50"/>

                <button type="button" ng-click="auth.signup()">Sign Up</button>

</form>

我有一个 Controller (没有错误),其功能看起来有点像这样:

    function SignupController($http, $scope, $rootScope, $location) {

          vm.signup = function(name, email, password, onSuccess, onError) {

              $http.post('http://myapi.com/api/authenticate/signup',
              {
                  name: name,
                  email: email,
                  password: password
              }).then(function(response) {
                // etc 
              }); 
}

现在,如何将表单中的名称、电子邮件、密码值分配给 Controller 中的名称、电子邮件、密码?

谢谢。

最佳答案

当您使用 ng-model="email"设置输入时,您只需调用 $scope.email 即可访问 Controller 中的这些变量值。

Case-1: For single value

HTML

<input type="text" ng-model="email" />

Angular Controller

console.log($scope.email);

Case-2: For multiple values

HTML

<input type="text" ng-model="user.firstname" />
<input type="text" ng-model="user.lastname" />
<input type="text" ng-model="user.email" />

Angular Controller

//This will print the all the values (firstname, lastname, email) contains user as object.
console.log($scope.user);

请检查此工作片段以查看实时示例

var app = angular.module('myApp', []);
app.controller('FormCtrl', function ($scope, $http) {
  $scope.user = {};
  $scope.signup = function(){
      console.log($scope.user);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <form name="signupForm" ng-controller="FormCtrl" ng-submit="signup()">
  <input type="text" placeholder="Name..." ng-model="user.name" name="name" required />
  <input type="email" name="email" ng-model="user.email" placeholder="Email..." required>
  <input type="password" placeholder="Password..." ng-model="user.password" name="password" required ng-minlength="7" ng-maxlength="50"/>
  <button type="submit">Sign Up</button>
  </form>
</body>

关于javascript - Angular - 在 Controller 中使用表单发布值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488870/

相关文章:

javascript - 使用 JavaScript 查找下一个标题的标记名和索引

javascript - 如何修改 AngularJS 中 $http 的接口(interface)?

AngularJS - "Select All"当前可见的项目

html - 如何使 angular-sanitize 不从 html 中删除 iframe 标签

javascript - 如何在输入中的任意位置单击打开 HTML 日期选择器对话框?

javascript - 当没有表单名称时如何清除AngularJS中的表单

javascript - 我应该如何加载包含我的 Controller 的所有 javascript 文件?

javascript - Div 标签背景颜色 CSS HTML

javascript - 设置 Jmeter 全局 javascript 对象

php - 提交网络表单时,不要重置数据字段