0

我正在尝试使用 cookie 值设置 ng-show 变量 isloggedout,该值正在工作(因此 $scope.isloggedout 为真或假),这应该触发显然不起作用的 ng-show 或 ng-hide。

我的应用控制器:

app.controller('projectCtrl', ['$scope', '$http', '$location', '$cookies', '$cookieStore', '$window',
function($scope, $http ,$location,$cookies,$cookieStore,$windows)   {
    $scope.isloggedout = $cookieStore.get('value');
}]);

我的 HTML:

<html ng-app="rjtApp" ng-controller="projectCtrl">
    <div >
    <ul class="nav navbar-nav navbar-right"  >
    <li><a href="#Signout" >Login <span class="glyphicon glyphicon-user"  ng-show="isloggedout"></span></a></li>
    <li><a href="#Login" >LogOut <span class="glyphicon glyphicon-user" ng-show="!isloggedout"></span></a></li></ul>
    </div>
</html>

有什么帮助吗?

4

1 回答 1

0

我尝试了您的代码,但我不能很好地理解您为什么这样做,仅在跨度中使用 ng-show 指令。我可以认为您想要显示或隐藏一个按钮,所以我调整了您的 html:

<html ng-app="rjtApp" ng-controller="projectCtrl">
<body>
    <div>
        <ul class="nav navbar-nav navbar-right">
            <li ng-show="isloggedout"><a href="#Signout">Login <span class="glyphicon glyphicon-user"></span></a></li>
            <li ng-hide="isloggedout"><a href="#Login">LogOut <span class="glyphicon glyphicon-user"></span></a></li>
        </ul>
    </div>

<!-- You will need to import here your angular.js file and your personal js files. -->
</body>
</html> 

我还将建议您将 var 限制在控制器内,以免弄脏范围。控制器将是:

app.controller('projectCtrl', ['$scope', '$http', '$location', '$cookies', '$cookieStore', '$window', function($scope, $http ,$location,$cookies,$cookieStore,$windows)   {
    var self = this;
    self.isloggedout = $cookieStore.get('value');
}]);

和 HTML:

<html ng-app="rjtApp" ng-controller="projectCtrl as pc">
<body>
    <div>
        <ul class="nav navbar-nav navbar-right">
            <li ng-show="pc.isloggedout"><a href="#Signout">Login <span class="glyphicon glyphicon-user"></span></a></li>
            <li ng-hide="pc.isloggedout"><a href="#Login">LogOut <span class="glyphicon glyphicon-user"></span></a></li>
        </ul>
    </div>

<!-- You will need to import here your angular.js file and your personal js files. -->
</body>
</html> 
于 2015-12-09T18:47:56.733 回答