AngularJS $范围数据没有显示任何内容

时间:2014-11-08 02:18:13

标签: javascript angularjs angularjs-scope angularjs-controller

submitform()不会将for数据带到函数中。 Chrome控制台说 ReferenceError:$未定义。代码有什么问题吗? enter image description here



    var app = angular.module('app', []);
    app.controller('testimonialController', function($scope, $http) {
      $scope.formdata = {};
      $scope.submission = false;
      $scope.submitform = function($scope, $http) {
        $http({
          method: 'POST',
          url: 'sendmail.php',
          data: $.param($scope.formdata), // pass in data as strings
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          } // set the headers so angular passing info as form data (not request payload)
        }).
        success(function() {
          console.log("send successfuly");
        }).
        error(function() {
          console.log("It is failed");
        });
      };
    });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="contact-form" ng-controller="testimonialController" ng-app="app">
  <h1 id="contact">Contact</h1>
  <pre>Form data: {{formdata}}</pre>
  <p>Take a look around the site, Do you have something in mind you would like to let me know .You can contact me by filling the form below or email hello@athimannil.com</p>
  <form ng-submit="submitform()">
    <div class="form-wrap">
      <label for="name">Name</label>
      <input type="text" name="name" ng-model="formdata.name" placeholder="Name" required>
      <br>

    </div>
    <div class="form-wrap">
      <label for="email">Email</label>
      <input type="email" name="email" ng-model="formdata.email" placeholder="Email" required>
      <br>

    </div>
    <div class="form-wrap">
      <label for="comment">Message</label>
      <br>
      <textarea name="comment" id="comment" ng-model="formdata.comment" placeholder="Comment" cols="30" rows="10" required></textarea>
      <br>

    </div>
    <input type="submit" name="submit" value="Send">
  </form>
</div>
&#13;
&#13;
&#13; 使用时,这是控制台消息 enter image description here

$http({
    method  : 'POST',
    url     : 'sendmail.php',
    data    : $scope.formdata,  // pass in data as strings
    headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}).

1 个答案:

答案 0 :(得分:1)

data: $.param($scope.formdata)

这一行使用jQuery的$.param方法。如果你想使用它,你必须包括它