以下是我的记录:
$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/