从控制器更新值时出现 ng-if 问题
ng-if problems when updated value from controller
我正在使用AngularJS和Meteor,并且在身份验证方面遇到麻烦,我正在使用帐户密码包以使用帐户包方法,但实现了我自己的UI。
至于现在我的ng.html(查看)文件上有这个:
<div ng-if="$auth.currentUser">I am visible only for logged in users!
<br>
{{ $auth.currentUser.emails }}
<form>
<input type = "submit" value = "Logout" ng-click = "logout();">
</form>
</div>
<div ng-if="!$auth.currentUser">
<input type="email" name="loginEmail" ng-model = "loginEmail">
<input type="password" name="loginPassword" ng-model = "loginPassword">
<input type = "submit" value = "Login" ng-click = "login(loginEmail,loginPassword);" >
</div>
因此,如果有用户记录,则在 $auth.currentUser 的值中不为 null,第一个div 应该是可见的。主要问题是我调用 $parent.login(...) 使用控制器进行身份验证,我的控制器上有这个:
$scope.login = function(loginEmail, loginPassword) {
//event.preventDefault();
Meteor.loginWithPassword(loginEmail, loginPassword);
//location.reload();
//$route.reload();
}
登录函数结束后,$auth.currentUser 变量不为空,没关系,现在我们应该看到另一个div,但是,ng-if 不会再次检查 $auth.currentUser 的值,直到发生某些事情,比如我按下按钮两次或使用我的一些网页函数,例如在经过时更改某些div 的某些样式或属性。
所以我的问题是,有没有办法让 ng-if 在某些方法结束后再次检查值(因为这在注册和注销方法中也会发生)或变量已更新?
感谢您的时间和答案:)
你可以像这样使用回调,
Meteor. loginWithPassword(loginEmail, loginPassword, function (err, result) {
if (err) {
// TODO: Log error
} else {
Meteor.logoutOtherClients(function () {
$state.go('your_state_name'); //something like $state.go('home');
});
}
$scope.$apply(function () {
//you can set any other $scope variables using $scope.$apply
$scope.isAuthenticating = false;
});
});
loginWithPassword
方法可以采用第三个参数;一个回调函数。LoginWithPassword 是一个异步方法,所以当服务器给我们返回答案(并且回调被触发)时,我们需要通知 Angularjs 发生了一些事情。在回调函数中调用 $scope.$apply()。
相关文章:
- ng绑定和ng href问题.ng href未从控制器加载数据
- 我需要NG-IF/NG-SWITCH还是NG-SHOW&NG-HIDE
- 绑定的角度ng-max问题
- 如何应用带过滤器的ng if来过滤记录,并在同一页面中显示两个不同的视图
- 在 ng-if 编译后访问指令中的 DOM 元素
- 通过ng-if设置隐藏文本框的值
- AngularJS ng-if指令在移除元素之前,即使条件为false,也会短暂呈现
- 使用 ng-if 时 ng 类的标记行为
- Angularjs ng-options 问题仅适用于 Chrome 浏览器
- 为什么更改范围获胜'不能在ng if内部工作,而是通过函数调用AngularJS
- Angular2 ng if有什么问题
- 为什么myForm$提交的不适用于ng消息,但适用于ng if
- ng if不验证函数
- 将默认选项添加到ng-if
- 通过使用ng if更新ng repeat加载程序
- 从控制器更新值时出现 ng-if 问题
- 为什么ng-if在angularjs中会产生如此多的问题
- ng-if指令:What'这是问题所在
- Angular $scope和ng-if/ng-show问题.视图中的状态没有改变
- ng-if和网格平铺的问题