带有ng-click的AngularJS登录表单不起作用 [英] AngularJS Login form with ng-click not working
问题描述
我在这个 plunk 中写了一个基本的登录表单 http://plnkr.co/edit/xQEN1ZNN5ZEw1CSwNw97?p=preview(点击 Home 路径上的红色 Log into Dashboard 按钮).
I wrote a basic login form in this plunk http://plnkr.co/edit/xQEN1ZNN5ZEw1CSwNw97?p=preview (click on the red Log into Dashboard button on the Home route).
但是由于某种原因,我无法在我的 loginCtrl 控制器代码中触发 login() 方法.
However for some reason I cannot get the login() method to fire in my loginCtrl controller code.
为什么这个例子有效,而我的无效?http://plnkr.co/edit/H4SVl6?p=preview
Why is this example working, and mine is not ? http://plnkr.co/edit/H4SVl6?p=preview
相反,它所做的是一个老式的 URL 重定向,其中用户/密码参数作为表单变量传入.我不知道出了什么问题.
Instead what it's doing is an old-school URL redirect with the user/password parameters passed in as form variables. I can't figure out what's wrong.
这里是 LoginCtrl
代码以及 login-form.html
模板:
Here is LoginCtrl
code as well as the login-form.html
template :
(function () {
'use strict';
angular.module('routerApp').controller('LoginCtrl',
['$rootScope', '$scope', authenticate]);
function authenticate($rootScope, $scope, userService) {
var login = this;
login.loginUser = function () {
login.dataLoading = true;
//loginService.authUser(login.user, login.password); // TO DO !!!
};
login.test = function () {
var test = true;
};
}
})();
<div ng-show="error" class="alert alert-danger">{{error}}</div>
<form ng-submit="login.loginUser()" name="form">
<div class="form-group">
<label for="username">Username</label>
<i class="fa fa-key"></i>
<input type="text" name="username" id="username" class="form-control" ng-model="login.username" required />
<span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
</div>
<div class="form-group">
<label for="password">Password</label>
<i class="fa fa-lock"></i>
<input type="password" name="password" id="password" class="form-control" ng-model="login.password" required />
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" ng-disabled="form.$invalid || dataLoading" class="btn btn-danger">Login</button>
<img ng-if="login.dataLoading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA=="/>
</div>
</form>
在我的本地应用程序中,它通过 URL 发布旧式表单变量,我无法让它在 LoginCtrl
内部触发 login.loginUser
函数.
In my local app, it is posting the old-style form variables via the URL, and I cannot get it to fire the login.loginUser
function below inside LoginCtrl
.
提前谢谢你...
鲍勃
推荐答案
由于某些奇怪的原因(可能是 ui-router 版本控制),controllerAs
无法正常工作.
for some odd reason (perhaps ui-router versioning) the controllerAs
is not working.
戴安娜的回答是有效的,但它不使用 controlleras
语法.如果您仍想使用它,请将 ui-router
设置更改为:
Diana´s answer is valid, but it doesn´t use the controlleras
syntax.
If you still want to use it, change the ui-router
setting to:
.state('login', {
url: "/login",
templateUrl: "login-form.html",
controller: 'LoginCtrl as login',
})
这应该可以解决问题;)
That should do the trick ;)
检查一下:http://plnkr.co/edit/OCqNexVeFFxt4kUuEVc1?p=preview
这篇关于带有ng-click的AngularJS登录表单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!