![](/img/trans.png)
[英]angular js: Not able to pass value from controller to html of an included page
[英]unable to pass username and password to controller from the html page in angular js
我有兩個字段用戶名和密碼,我想在angular js控制器中獲取以驗證登錄名。 但是在控制器中,我在填寫用戶名和密碼並提交按鈕后無法獲取它
<form class="form-signin" action="" method="post">
<span id="reauth-email" class="reauth-email"></span>
<input type="text" ng-model="username" id="Username" name="Username" class="form-control" placeholder="Email address" required autofocus>
<input type="password" ng-model="password" id="password" name="password" class="form-control" placeholder="Password" required style="margin-top:10px">
<div id="remember" class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<input type="hidden" name="" value="" />
<input class="btn btn-lg btn-primary btn-block btn-signin" type="button" value="{{btntext}}" ng-click="login()" >
</form>
角js
var app = angular.module('homeapp', []);
app.controller('HomeController', function($scope, $http) {
$scope.username = '';
alert($scope.username);
$scope.btntext="Login";
$scope.login=function () {
$http.get("/account/loginverify")
.then(function(response) {
$scope.myWelcome = response.data;
if(response.data=="1"){
window.location.href='home.html'
}
else {
alert("Invalid username or password...!!!")
}
});
}
});
您需要使用ngController
指令將控制器綁定到HTML模板。 請參見以下示例:
var app = angular.module('homeapp', []); app.controller('HomeController', function($scope, $http) { $scope.username = ''; $scope.btntext = "Login"; $scope.login = function() { console.log($scope.username, $scope.password); //$http.get("/account/loginverify") // .then(function(response) { // $scope.myWelcome = response.data; // if (response.data == "1") { // window.location.href = 'home.html' // } else { // alert("Invalid username or password...!!!") // } // }); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <form class="form-signin" action="" method="post" ng-app="homeapp" ng-controller="HomeController"> <span id="reauth-email" class="reauth-email"></span> <input type="text" ng-model="username" id="Username" name="Username" class="form-control" placeholder="Email address" required autofocus> <input type="password" ng-model="password" id="password" name="password" class="form-control" placeholder="Password" required style="margin-top:10px"> <div id="remember" class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <input type="hidden" name="" value="" /> <input class="btn btn-lg btn-primary btn-block btn-signin" type="button" value="{{btntext}}" ng-click="login()"> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.