2014-12-03 83 views
0

我無法在$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 - 做這樣的事情最有角度的方式是什麼?

+2

'ng-hide =「logged」'? – 2014-12-03 11:57:13

+0

@NaeemShaikh,謝謝你的訣竅:) – sameera207 2014-12-03 21:13:09

回答

1

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

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

這就夠了。

+0

感謝它的工作原理,我可以刪除'$ rootScope。$ apply()',以及現在都好:) – sameera207 2014-12-03 21:12:46

+0

如果你正在更新,你只需要使用$ apply超出了角度的上下文(如果我的知識服務正確)例如jQuery,如果你在一個範圍對象,你不應該需要調用apply – Lewis 2017-12-05 22:03:12