javascript - AngularJS $scope 数据没有显示任何内容

标签 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>
这是使用时的控制台消息 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)
}).

最佳答案

数据:$.param($scope.formdata)

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

关于javascript - AngularJS $scope 数据没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813011/

相关文章:

javascript - 抓取动态数据

Javascript 从引用者中解析 id

angularjs - 使用 AngularFire 根据 id 连接路径之间的数据

javascript - 如何使用跨越多列的单列为表格行设置动画?

javascript - 来自 jquery 插件的 angularjs 指令

触发自定义验证指令后,AngularJS ng-model 值丢失

javascript - 在Angularjs中使用rest api上传图像

javascript - 非常敏感的 onmouseout 行为

javascript - 如何在另一个对象中复制对象属性?

javascript - 为什么当用户关闭 chrome 中的选项卡时弹出?