[英]AngularJS ng-show and ng-hide doesn't work
在這里遇到angularjs邏輯問題
我只想在沒有用戶登錄時顯示登錄鏈接,而僅顯示登錄用戶的注銷鏈接,但無法正常工作
我的HTML
<section ng-controller="a" ng-show="auth = false">
<a href="#/signin" ng-click="signin()"> Sign in </a>
</section>
<section ng-controller="b" ng-show="auth = true">
<a href="#/signout" ng-click="signout()"> Sign out </a>
</section>
登錄工作正常
這是我的控制器
登錄控制器
function ($scope, Service){
$scope.auth = true;
$scope.signin = function($event, userID, passwd){
Service.signin(userID,passwd).then(
function (response){
$scope.auth = true;
});
}]).
注銷控制器
function ($scope, Service){
$scope.auth = false;
$scope.signout = function($event){
Service.signout().then(
function (response){
$scope.auth = false;
});
}]).
那兩個注銷和登錄鏈接基本上在我的主頁上。 我不想創建很多頁面,因此我想彼此隱藏。 當用戶單擊“登錄”鏈接時,它將運行angularjs路由器,在本例中為/ login,該表單還有另一個templateURL,它將直接附加到主頁上。 用戶輸入用戶名和密碼后,用戶需要單擊“提交”按鈕,這是代碼
<form role="form" name="form1">
<label for"userID">UserID</label><input type="text" id="userID" ng-model="userID">
<label for"passwd">Password</label><input type="text" id="passwd" ng-model="passwd">
<button data-ng-click="signin($event,userID,passwd); reload()">Login</button>
reload()函數將直接刷新頁面。 我正在使用$ window.location.reload()
您的均等比較不正確。
這為auth分配了false值:
ng-show="auth = false"
您想要的是(雙倍和三倍等於進行比較)
ng-show="auth === false"
您也可以這樣做:
<section ng-controller="a" ng-show="!auth">
<a href="#/signin" ng-click="signin()"> Sign in </a>
</section>
<section ng-controller="b" ng-show="auth">
<a href="#/signout" ng-click="signout()"> Sign out </a>
</section>
實際上,您需要完成兩項工作才能完成此任務
您需要在$rootScope
分配auth
varibale而不是$scope
。 因為此對象在兩個控制器中使用。
喜歡
function ($scope, dataService, $rootScope){
$scope.auth = true;
$scope.signin = function($event, userID, passwd){
Service.signin(userID,passwd).then(
function (response){
$rootScope.auth = true;
});
}]).
您需要將ng-show="auth = false"
更改為ng-show="auth === false"
。 您使用了單等號。 它應該是兩倍或三倍相等
要么
如果在$ rootScope中分配了該對象,則無需檢查元素中條件為true還是false。 因為您已經在控制器中定義auth
為false或true。 因此,您只能調用ng-show="auth
僅。
只需嘗試以下
ng-show="auth"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.