1
返回空值我有一个这样的控制器:登录控制器上后
var myApp = angular.module('myApp', ['ui.router']);
myApp.controller('loginController',['$http', function($http)
{
this.loginForm = function(){
console.log(this.email); //prints undefined
console.log(this.password); //prints undefined
var encodedString = 'email=' +
encodeURIComponent(this.email) +
'&password=' +
encodeURIComponent(this.password);
$http({
method:'POST',
url: 'rs/loginResource',
data: encodedString,
headers: {'Content-Type' : 'application/x-www-form-urlencoded'}
});
};
}]);
凡this.email
和this.password
返回undefined。
我的形式:
<!-------------- loginController declared here (ERROR when declared)------------------->
<div class="row" ng-controller="loginController as loginCtrl">
<div class="col-md-12">
Login via
<div class="social-buttons">
<a href="#" class="btn btn-fb"><i class="fa fa-facebook"></i> Facebook</a>
<a href="#" class="btn btn-google" ><i class="fa fa-google"></i> Google</a>
</div>
or
<!------------------------------------ Here is the form ---------------------------------------------------------->
<form class="form" role="form" method="post" ng-submit="loginCtrl.loginForm()" accept-charset="UTF-8" id="login-nav">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail2" ng-model="loginCtrl.inputData.email">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail2" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2" ng-model="loginCtrl.inputData.password">Password</label>
<input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password" required>
<div class="help-block text-right"><a href="">Forget the password ?</a></div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Sign in</button>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> keep me logged-in
</label>
</div>
</form>
<!------------------------------------ End of form ---------------------------------------------------------->
</div>
<div class="bottom text-center">
New here ? <a href="#"><b>Join Us</b></a>
</div>
</div>
试图在打印控制台console.log(this.email); console.log(this.password);
它打印undefined
两个值。有没有其他的方式来捕捉角度值
'this'的'$ http.get'函数内部是不一样的控制器的'this' http://stackoverflow.com/a/34474833/2435473 –
在您的形式使用'纳克-model = “loginCtrl.inputData.email”'。尝试记录'inputData'对象。 – georgeawg