ng根据作用域值隐藏

ng-hide based on scope values

本文关键字:隐藏 作用域 ng      更新时间:2023-09-26

我想根据登录信息隐藏列表值。当超级管理员登录显示城市列表时。但用户的登录隐藏名单。

这是controller.js

  myAppControllers.controller('LoginController',['$scope', '$location', '$http', 
function($scope, $location, $http) {
    $scope.log_userx = {};
    $scope.login_user = function(login) {
    $scope.log_user = angular.copy(login);    
    $scope.login = {};                                  
    $http({
        url: "/login",
        method: "POST",
        headers: { 'Content-Type': 'application/json' },
        data:$scope.log_user
    }).success(function (data) {
        var resp = data['user']
        $scope.user_admin = data.user.roles;
        $location.path("/index");
    }).error(function (data,status) {
        $location.path("/login");
    });
    $scope.finduser=function(){
    $scope.user_admin1=$scope.user_admin;
    alert(angular.toJson("admin1 "+$scope.user_admin1));
     $scope.visible = false;
     if($scope.user_admin1!=="superadmin")
     {
     alert("Not Superadmin");
     $scope.visible =true;
     return $scope.visible;
     }
    return true;
    };
};}]);

在Html文件中

        <ul class="breadcrumb" style="background-color:#EEEEEE" ng-init="finduser()">
            <li><button type="submit" id="datadash" class="btn btn-default">home</button></li>
            <li ng-if="visible==true"><a href="/#/city" class="button special">city</a></li>            
</div>

使用if和else条件更新作用域。

if($scope.user_admin1!=="superadmin")
 {
 alert("Not Superadmin");
 $scope.visible =true;
// return $scope.visible;
 }else{
$scope.visible =false;
}

并按如下方式更改HTML。

<li ng-if="visible"><a href="/#/city" class="button special">city</a></li>

尝试通过更改查看城市列表的条件来使用此html。

<ul class="breadcrumb" style="background-color:#EEEEEE" ng-init="finduser()">
        <li><button type="submit" id="datadash" class="btn btn-default">home</button></li>
        <li ng-if="!visible"><a href="/#/city" class="button special">city</a></li>
</ul>

您应该为此使用ng-show。

<li ng-show="visible==true"><a href="/#/city" class="button special">city</a></li>

有了这个唯一的"城市"链接将只出现在您的超级管理员。

显示这个superadmin链接是基于controller.js级别的$scope值的动画的一部分,而ng-if在html到html的输入中有动画时使用$animate服务,排除controller.js