Javascript 如果所有数组项都隐藏,则在AngularJS中显示另一个div
我在页面上使用了ng repeat<代码>ng类运行良好Javascript 如果所有数组项都隐藏,则在AngularJS中显示另一个div,javascript,angularjs,ionic-framework,Javascript,Angularjs,Ionic Framework,我在页面上使用了ng repeatng类运行良好 <div class="card news" ng-repeat="item in news track by $index" id="{{news.nid}}" ng-init="parentIndex = $index" ng-class="{hidden: '{{getCheck($index)}}' == 'true'}"> ... </div> ... 现在我需要,如果所有项目都被隐藏
<div class="card news" ng-repeat="item in news track by $index" id="{{news.nid}}" ng-init="parentIndex = $index" ng-class="{hidden: '{{getCheck($index)}}' == 'true'}">
...
</div>
...
现在我需要,如果所有项目都被隐藏,显示这个div:
<h3 class="news-empty">No news</h3>
没有新闻
规则是什么?我怎么做?谢谢。您希望在h3标记中添加一个,并将其指向您在控制器中编写的一个函数,该函数可以检查数组是否为空,可能是通过迭代隐藏的同一数组并在每个数组上运行getCheck($index) 您需要另一种方法来检查是否隐藏了所有元素:
$scope.everythingIsHidden = function() {
return $scope.news.every((new, index) => $scope.getCheck(index));
}
$scope.getCheck = function(index) { // Your getChek function that I suppose it checks if an element is hidden based on index
//...
}
<h3 class="news-empty" ng-if="everythingIsHidden()">No news</h3>
$scope.everythingishiden=function(){
返回$scope.news.every((新建,索引)=>$scope.getCheck(索引));
}
$scope.getCheck=function(index){//您的getChek函数,我想它会根据索引检查元素是否隐藏
//...
}
没有消息
组委会的答案将起作用。如果你想以一种更“有角度”的方式来做这件事,你需要重构你所拥有的
你不应该试图用CSS类来隐藏它们。ngRepeats具有内置的筛选器语法。所以,你应该把它们过滤掉
<div class="card news"
ng-repeat="item in news | filterMethod as results track by $index"
id="{{news.nid}}"
ng-init="parentIndex = $index"
>
<h3 class="news-empty" ng-if="results.length === 0" >No news</h3>
没有消息
repeat中的as results
语句将过滤后的数组存储在results中filterMethod
需要是一个函数,它的工作原理可能与您的getCheck($index)
方法类似