简体   繁体   English

AngularJS ng-show和ng-hide不起作用

[英]AngularJS ng-show and ng-hide doesn't work

having a problem with angularjs logic here 在这里遇到angularjs逻辑问题

I want to show the login link only when there is no user loggedin, and only show the logout link to logged in user, but it doesnt work properly 我只想在没有用户登录时显示登录链接,而仅显示登录用户的注销链接,但无法正常工作

my HTML 我的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>

The sign in is working well 登录工作正常

this is my Controller 这是我的控制器

login Controller 登录控制器

 function ($scope, Service){
 $scope.auth = true;
 $scope.signin = function($event, userID, passwd){ 

                 Service.signin(userID,passwd).then( 
                        function (response){
                            $scope.auth = true; 
                  });
 }]).

logout Controller 注销控制器

  function ($scope, Service){
  $scope.auth = false; 
  $scope.signout = function($event){ 
                 Service.signout().then( 
                        function (response){
                            $scope.auth = false;    
                  });
  }]).

those 2 log out and log in links are basically in my main page. 那两个注销和登录链接基本上在我的主页上。 I dont want to create a lot of pages, therefore I want to hide each other. 我不想创建很多页面,因此我想彼此隐藏。 When the user click the log in link, it will run the angularjs router, in this case /login, there is another templateURL for the form and it will be appended directly to the main page. 当用户单击“登录”链接时,它将运行angularjs路由器,在本例中为/ login,该表单还有另一个templateURL,它将直接附加到主页上。 Once the user has typed in the userID and password, the user need to click submit button, this is the code 用户输入用户名和密码后,用户需要单击“提交”按钮,这是代码

<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>

the reload() function will directly refresh the page. reload()函数将直接刷新页面。 I am using the $window.location.reload() 我正在使用$ window.location.reload()

Your equals comparison is incorrect. 您的均等比较不正确。

This is assigning a value of false to auth: 这为auth分配了false值:

ng-show="auth = false"

What you want is this (double and triple equals do comparisons) 您想要的是(双倍和三倍等于进行比较)

ng-show="auth === false"

You can also do this: 您也可以这样做:

<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>

Actually You need Two works for this task 实际上,您需要完成两项工作才能完成此任务

First Work:- 首先工作:

You need to assign auth varibale in $rootScope not $scope . 您需要在$rootScope分配auth varibale而不是$scope because this object used in two controller. 因为此对象在两个控制器中使用。

Like 喜欢

function ($scope, dataService, $rootScope){
 $scope.auth = true;
 $scope.signin = function($event, userID, passwd){ 

                 Service.signin(userID,passwd).then( 
                        function (response){
                            $rootScope.auth = true; 
                  });
 }]).

Second Work: 第二工作:

You need to change ng-show="auth = false" to ng-show="auth === false" . 您需要将ng-show="auth = false"更改为ng-show="auth === false" You have used single equal. 您使用了单等号。 it should be double or triple equal 它应该是两倍三倍相等

OR 要么

if you assigned the object in $rootScope, then you don't need to check the condition for is true or false in the element. 如果在$ rootScope中分配了该对象,则无需检查元素中条件为true还是false。 because you already define auth is false or true in your controller. 因为您已经在控制器中定义auth为false或true。 So you can just call ng-show="auth only. 因此,您只能调用ng-show="auth仅。

Just try the following 只需尝试以下

ng-show="auth"

Please let me know if it is still not working.. 请让我知道它是否仍然无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM