2016-03-30 70 views
0

按下按钮,浏览器不会发生任何操作,并且我的REST端点上没有新用户。 registerUser()由按钮按下触发。我密切关注教程。AngularJs Http发布不发帖

的HTML部分

<div class="section no-pad-bot" ng-controller="registerController" id="index-banner"> 

等等等等然后

   <form> 
        <div class="row"> 
         <div class=" input-field col s6 offset-s3"> 
          <i class="material-icons prefix">account_circle</i> 
          <input type="text" class="validate" ng-model="user.username" placeholder="Username"> 
         </div> 
        </div> 
        <div class="row"> 
         <div class=" input-field col s6 offset-s3"> 
          <i class="material-icons prefix">account_circle</i> 
          <input type="text" class="validate" ng-model="user.email" placeholder="Email"> 
         </div> 
        </div> 
        <div class="row"> 
         <div class=" input-field col s6 offset-s3"> 
          <i class="material-icons prefix">account_lock</i> 
          <input type="text" class="validate" ng-model="user.password" placeholder="Password"> 
         </div> 
        </div> 
        <button class="btn waves-effect waves-light" type="submit" ng-click="registerUser()">Submit 
            <i class="material-icons right">send</i> 
           </button> 
       </form> 

在clientapp.js

myApp.controller('registerController', function ($scope, $http) { 
    $scope.registerUser = function() { 
      // use $.param jQuery function to serialize data from JSON 
      var data = $.param({ 
       hashword: $scope.password, 
       username: $scope.username, 
       email: $scope.email, 
      }); 

      var config = { 
       headers : { 
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' 
       } 
      } 

      $http.post('/api/users', data, config) 
      .success(function (data, status, headers, config) { 
       console.log("successful post"); 
       $scope.PostDataResponse = data; 
      }) 
      .error(function (data, status, header, config) { 
       console.log("failed post"); 
       $scope.ResponseDetails = "Data: " + data + 
        "<hr />status: " + status + 
        "<hr />headers: " + header + 
        "<hr />config: " + config; 
      }); 
     }; 
+3

我不明白'你的范围user'对象,改变'NG-模型= “user.email”'到NG-模型=“电子邮件”类似地'ng-model =“user.username”'将'ng-model =“username”'和'ng-model =“user.password”'转换为'ng-model =“password”' – manish

回答

2

因为在HTML页面中,你是指user.username,“用户。电子邮件'和user.password而不是username,分别为和password

尝试改变下列

<input type="text" class="validate" ng-model="username" placeholder="Username"> 
<input type="text" class="validate" ng-model="email" placeholder="Email"> 
<input type="text" class="validate" ng-model="password" placeholder="Password"> 

希望它可以帮助

+0

谢谢,但仍然由于某种原因,鼠标点击按钮什么都不做 –

+0

对不起,没有任何意义?尝试在浏览器的Developer Console中查看有效内容。 –