javascript - 根据 ng-repeat 内部的条件显示/隐藏记录不起作用

标签 javascript angularjs

以下是我的记录:

$scope.currentSelection = "Admin";

$scope.records = [
     { id: 'Employee', params:[]},
     { id: 'Skills', params:[]},
     { id: 'Payroll', params:[]},
     { id: 'Appraisal', params:[]},
     { id : 'Statistics',params:[]}
];

现在,如果 $scope.currentSelection 是 admin,那么我想显示带有 statistics(Employee,Skills,Payroll,Appraisal,Statistics) 的所有项目,否则我不想显示统计信息其余其他项目(员工、技能、薪资、评估)如果 $scope.currentSelection 是访客或员工。

但问题是,当 currentSelection 为 Admin 时,除统计信息外,其他项目不会显示。

注意:我不想为此使用过滤器。

但是问题

var app = angular.module("myApp", []);
app.controller("MyController", function ($scope) {
     $scope.currentSelection = "Admin";
     $scope.records = [
          { id: 'Employee', params:[]},
          { id: 'Skills', params:[]},
          { id: 'Payroll', params:[]},
          { id: 'Appraisal', params:[]},
          { id : 'Statistics',params:[]}
     ];
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <ul ng-app="myApp" ng-controller="MyController">
     <li ng-repeat="item in records">
         <div ng-show="currentSelection=='Admin' && item.id=='Statistics'">{{item.id}}
         </div>
    </li>
</ul>

最佳答案

您的支票有误,

1) 如果您想显示每个列表项,则应为 ng-show="currentSelection=='Admin'

2) 应该是 ng-show="currentSelection=='Guest ' ||currentSelection=='Employee' && item.id!=='Statistics'" 如果您想显示除统计信息之外的所有列表项。

var app = angular.module("myApp", []);
        app.controller("MyController", function ($scope) {
            $scope.currentSelection = "Admin";

 $scope.records = [
{ id: 'Employee', params:[]},
{ id: 'Skills', params:[]},
{ id: 'Payroll', params:[]},
{ id: 'Appraisal', params:[]},
{ id : 'Statistics',params:[]}
];

        });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyController">
<ul ng-show="currentSelection=='Admin'">
    <li ng-repeat="item in records">
        {{item.id}}
    </li>
</ul>
<ul ng-show="currentSelection=='Guest' || currentSelection=='Employee'">
     <li ng-repeat="item in records" ng-if="item.id!=='Statistics'">
         {{item.id}}
    </li>
</ul>
</div>

关于javascript - 根据 ng-repeat 内部的条件显示/隐藏记录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44919809/

相关文章:

javascript - 在 Windows 7 上安装 contextify 错误 : `gyp` failed with exit code: 2

Javascript 滚动

javascript - 如果月份与特定月份匹配,则在选择某个月份时尝试显示多行数据

javascript - AngularJS:如何隐藏 Controller div,然后在同一位置显示另一个 Controller div?

javascript - 如何在angularjs中迭代一个对象?

javascript - 如何替换js中的html标签?

javascript - 内联列表中的元素未对齐

javascript - 多线程的 node.js 版本?

javascript - AngularJS - 如何从另一个表单向下钻取设置文本输入?

javascript - 播放 m3u8 文件时 cordova-plugin-media 出错