Javascript 为什么赢了';t Angular JS控制器上的范围变量设置为true?
我正在尝试编写一个控制器,以便在另一个控制器发生更改后,某些输入被禁用 这是控制器:Javascript 为什么赢了';t Angular JS控制器上的范围变量设置为true?,javascript,angularjs,Javascript,Angularjs,我正在尝试编写一个控制器,以便在另一个控制器发生更改后,某些输入被禁用 这是控制器: app.controller('SignUpController',function ($scope, $http) { this.unavaliable = true this.userUnavaliable = function() { console.log(this.unavaliable) return this.unavaliable }
app.controller('SignUpController',function ($scope, $http) {
this.unavaliable = true
this.userUnavaliable = function() {
console.log(this.unavaliable)
return this.unavaliable
}
this.userExists = function(mail) {
if (mail) {
var who = $http.get("/existingUsers/"+mail)
who.success(function(data,status, headers, config) {
if (data.mail) {
this.unavaliable = true
console.log(data.mail + " ya existe en la DB")
}
else{
this.unavaliable = false
}
});
who.error(function(data, status, headers, config) {
alert("AJAX failed!");
})
}
}
})
正如我下面的标记所示,当unavailable
设置为true
时,一个输入应该获得某个类,另一个输入应该被禁用。但即使我可以访问控制台.log()
,变量似乎也永远不会为真
这是我的标记:
<form class="form-inline" role="form">
<div class="form-group">
<input type="email" class="form-control input-lg" ng-model="signup.mail" placeholder="e-mail" ng-change="signup.userExists(signup.mail)" ng-class="{'has-error':signup.userUnavaliable()}">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="Contraseña" ng-nodel="signup.password">
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="signup.role" value="admin"> Administrador
</label>
</div>
<button type="submit" class="btn btn-primary" ng-disabled="signup.unavaliable" >Registrar</button>
</form>
管理员
登记员
我尝试了使用$scope
而不是此
,但始终没有成功尝试以下方法:
app.controller('SignUpController',function ($scope, $http) {
var that = this;
that.unavaliable = true;
that.userUnavaliable = function() {
console.log(that.unavaliable)
return that.unavaliable
}
that.userExists = function(mail) {...
您的问题似乎与JS上下文有关;在上面的示例中,它保存在
变量中。这就是在中如何在某个点上完成的
这个可能不是你所认为的,很可能你正在尝试约翰·帕帕(John Papa)所揭示的这种奇怪的方法,但你需要,在读到我真的不明白我应该做什么之后,在@Dalorzo之前将其设置为var。你能解释一下吗?你太棒了!成功了。你能用苹果给我解释一下吗。它可能会在将来帮助其他学习者