2017-03-29 3 views
0

入力フィールドにユーザー名とパスワードを入力し、API URLに送信してユーザーを認証し、データを返そうとしています。 URLにハードコードされた資格情報を提供し、正常にログインできます。しかし、私が必要とするのは、Webインターフェースで値を動的に入力してログインを実行できることです。角度jsのログインを実装する方法:REST API URLへのバインディングと入力値?

入力値をURLにバインドする方法とその形式を書き込む方法がわかりません。これで助けてください。

ハードコーディングされたユーザー名とパスワードを使用して私のコントローラ:

var login = angular.module('login', []); 
    login.controller('loginController',  ['$scope','$http','$location',function($scope, $http) { 
    $scope.loginauth = function(username,password) 
    { 
      $http({ 
      method : 'POST', 
      url  : 'http://televisionarybackendservices.azurewebsites.net/auth/login?username=Evangeli&password=1234', 
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
      }) 
      .success(function(data) { 
       console.log(data) 
       $scope.userData = data; 

     }); 
    }; 
enter code here

}])。

ログインするための私のHTMLモーダル。

<div id="loginModal" class="modal fade" role="dialog" ng-controller="loginController"> 
    <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal">&times;</button> 
        <h4 class="modal-title">Login</h4> 
       </div> 
       <div class="modal-body"> 
        <form class="form" action="" method="POST" enctype="multipart/form-data" role="form"> 
        <div class="form-group"> 
        <label for="ue">Username</label> 
        <input type="text" class="form-control" id="ue" placeholder="" ng-model="username"> 
        </div> 
        <div class="form-group"> 
        <label for="pwd">Password</label> 
        <input type="password" class="form-control" id="pwd" placeholder="" ng-model="password"> 
        </div> 
        </form> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-success" ng-click="loginauth(username,password)">Login</button> 
        <button type="button" class="btn btn-success" data-dismiss="modal">Close</button> 
        <div ng-repeat="data in userData"> 
         {{data.username}}<br /> 
         {{data.created_at}} 
        </div> 

       </div> 
      </div> 
    </div> 

私は動的にユーザー名とパスワードを入力し、ハードコードされた値を供給しないようにする必要があります。前もって感謝します。

+0

文法と構造を編集してより明確にしました。 –

答えて

0

私は答えを得ました。これは助けが必要な人のためです。

var login = angular.module('login', []); 
    login.controller('loginController', ['$scope','$http','$location',function($scope, $http) { 
    $scope.loginauth = function(username,password) 
    { 
      $http({ 
      method : 'POST', 
      url  : 'http://televisionarybackendservices.azurewebsites.net/auth/login', 
      data: jQuery.param({ 
     username: $scope.username, 
     password: $scope.password 
       }), 
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
      }) 
      .success(function(data) { 
       console.log(data) 
       $scope.userData = data; 

     }); 
    }; 

}]);

関連する問題