角度接触形式 - 发送后的未定义字段

时间:2016-05-17 11:45:15

标签: angularjs forms email undefined

这是我的index.html

  <div data-ng-controller="help">
<div id="messages" class="alert alert-success" data-ng-show="messages" data-ng-bind="messages"></div>
<div data-ng-show="progress.active()" style="color: red; font-size: 50px;">Sending&hellip;</div>
<form name="helpForm" novalidate role="form">
  <div class="form-group">

    <label for="name">Your Name </label>
    <span class="label label-danger" data-ng-show="submitted && helpForm.name.$error.required">Required!</span>
    <input type="text" name="name" data-ng-model="userName" class="form-control" required />
  </div>

  <div class="form-group">
    <label for="email">Your E-mail address</label>
    <span class="label label-danger" data-ng-show="submitted && helpForm.email.$error.required">Required!</span>
    <span class="label label-danger" data-ng-show="submitted && helpForm.$error.email">Invalid email!</span>
    <input type="email" name="email" data-ng-model="userEmail" class="form-control" required />  
  </div>

  <div class="form-group">
    <label for="message">Message</label>
    <span class="label label-danger" data-ng-show="submitted && helpForm.message.$error.required">Required!</span>
    <textarea name="message" data-ng-model="userMessage" class="form-control" required></textarea>
  </div>

  <button data-ng-disabled="progress.active()" data-ng-click="submit(helpForm)" class="btn btn-default">Submit</button>
</form>

我app.js的一部分

angular.module('myApp', ['ajoslin.promise-tracker'])


.controller('help', function ($scope, $http, $log, promiseTracker, $timeout) {
    $scope.subjectListOptions = {
      'bug': 'Report a Bug',
      'account': 'Account Problems',
      'mobile': 'Mobile',
      'user': 'Report a Malicious User',
      'other': 'Other'
    };

$scope.progress = promiseTracker();

$scope.submit = function(form) {

      $scope.submitted = true;


      if (form.$invalid) {
        return;
      } 



var $promise = $http({
        method: 'POST',
        url:'api/contactUs',
        params: {
          'callback' : 'JSON_CALLBACK',
          'userName' : $scope.userName,
          'userEmail' : $scope.userEmail,
          'userMessage' : $scope.userMessage
      }

  })
      .success(function(data, status, headers) {
      if (data.status == 'OK') {
        $scope.userName = null;
        $scope.userEmail = null;
        $scope.userMessage = null;
        $scope.messages = 'Your form has been sent!';
        $scope.submitted = false;
      } else {
        $scope.messages = 'Oops, we received your request, but there was an error processing it.';
        $log.error(data);
      }
    })
    .error(function(data, status, headers) {
      $scope.progress = data;
      $scope.messages = 'There was a network error. Try again later.';
      $log.error(data);
    })
    .finally(function() {

      $timeout(function() {
        $scope.messages = null;
      }, 9000);
    });

将信件发送到邮件后,表单中的所有字段(姓名,电子邮件,邮件)都显示为“未定义”

我使用node.js服务器并通过api(api / contactUs)连接

我无法理解问题是什么,我已尽力尝试,但错误仍然存​​在。

请帮助解决此问题。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

使用POST方法时,应该传递data个对象而不是params个对象。

改变这个:

params: {
    'callback' : 'JSON_CALLBACK',
    'userName' : $scope.userName,
    'userEmail' : $scope.userEmail,
    'userMessage' : $scope.userMessage
}

对此:

data: {
    'callback' : 'JSON_CALLBACK',
    'userName' : $scope.userName,
    'userEmail' : $scope.userEmail,
    'userMessage' : $scope.userMessage
}