2013-03-31 29 views
0

只需進入來自Zend Framework 2/jQuery工作方式的AngularJS即可。AngularJS - 用於交換登錄/我的賬戶部分的變量

我的Zend框架內的layout.html將包含這樣的內容:

<html><body> 
<div id="topNavigation" class="nav nav-fixed"> 
<?php 
if ($this->identity()) : 
    ?> 
    <button id="logoutBtn">Log out</button> 
    ?> 
else: 
    ?> 
    <input type="text" name="username" /> 
    <input type="password" name="password" /> 
    <button>Login</button> 
    <?php 
<?php 
endif; 
?> 
</div> 
<?php 
// The content from each ViewAction 
echo $this->content 
?> 
<footer>This is the common footer text.</footer> 
</body></html> 

現在我把它改爲AngularJS我理解中的index.html是ngView指令指向模板文件爲每個視圖佈局。 如何在我的佈局中獲取變量以切換登錄狀態?如果你不能真正控制佈局,我在哪裏持有保持登錄狀態的代碼,因爲我沒有看到任何地方的全局代碼。

我angularJS index.html的佈局文件是目前像上面:

<html><body> 
<div id="topNavigation" class="nav nav-fixed"> 
    <button id="logoutBtn">Log out</button> 
    <input type="text" name="username" /> 
    <input type="password" name="password" /> 
    <button>Login</button> 
</div> 
<div ng-view></div> 
<footer>This is the common footer text.</footer> 
</body></html> 

回答

1

最好的地方大概是$ rootScope其​​中,因爲所有控制器的範圍從這個繼承,可以被看作是「全球」 。

我在你的index.html上面看到,雖然你沒有指定控制器。您需要添加一個控制器(可能在'body'中),並在該控制器中將登錄信息添加到$ rootScope。

一旦你在$ rootScope中有一個變量(比如'isLoggedIn'),那麼你可以使用ng-show和ng-hide指令來確定你想要顯示的DOM的子集。

+0

啊對不起,我沒有添加控制器的東西。我認爲ngView是你的控制器模板加載的地方。因此,沒有任何地方可以放置這樣的東西,而只需在每個控制器中引用$ rootScope。嗯不錯!謝謝:) –

+1

HTML中的任何元素都可以有一個控制器,它非常方便。如果在這些元素的子元素中定義了另一個控制器,則該控制器將從其父元素繼承。 –

相關問題