2016-03-30 113 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中查看有效內容。 –