【问题标题】:Not reflect my changes in `$rootScope` in angularjs不反映我在 angularjs 中的 `$rootScope` 的变化
【发布时间】:2017-12-06 06:01:19
【问题描述】:

我无法在$rootScope 中反映我的更改。据我了解(请注意,我对angular 很陌生),$rootScopecontrollers 中可见。我要做的是更新$rootScope 中的值并尝试在我的html 页面中读取该值。

问题是即使更改已应用于$rootScope,但在我看来并没有反映出来。以下是代码

我有一个div,我想根据值显示/隐藏它

#view
<div ng-hide="{{logged}}">
  // this should visible only if the logged is true
</div>  



#controller1 I set the rootscope to false
$rootScope.logged = false; // this makes the view hidden


#controller2 I set the rootScope to true
$rootScope.logged = true; 
$rootScope.$apply();

但是制作$rootScope.logged = true 不会使视图可见。我的问题是

1 - 我可能出了什么问题?

2 - 做这种事情最 Angular 的方式是什么?

【问题讨论】:

  • ng-hide="logged" ?
  • @NaeemShaikh,谢谢你成功了 :)

标签: javascript angularjs rootscope


【解决方案1】:

无需在您的 html 模板中使用{{

#view
<div ng-hide="logged">
  // this should visible only if the logged is true
</div>

这就够了。

【讨论】:

  • 谢谢它的工作,我也可以删除$rootScope.$apply(),现在一切都很好:)
  • 你只需要使用 $apply 如果你更新的角度上下文之外(如果我的知识是正确的)例如 jquery,如果你在一个范围对象中,你不需要调用 apply
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
相关资源
最近更新 更多