gpt4 book ai didi

javascript - Angular js - 如果 ng-repeat 为空则显示消息

转载 作者:行者123 更新时间:2023-12-03 00:10:58 25 4
gpt4 key购买 nike

以下 AngularJS 应用程序正在使用 ng-repeat 和应用的过滤器。某个应用的过滤器没有留下任何值。如何显示通知?

js fiddle

HTML

<div >
<div data-ng-controller="myCtrl">


<ul >
<li data-ng-repeat="item in values | filter:filterIds()">
<code>#{{item.id}}</code> Item
</li>
</ul>

<p ng-show="!values.length">no vals with this filter</p>
<button ng-click="loadNewFilter()"> filter now</button>
</div>


</div>

AngularJS

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
$scope.values = [{
id: 1
}, ....
}];

$scope.filter = [1,2,3,4,5,6];

$scope.filterIds = function (ids) {
return function (item) {
var filter = $scope.filter;

return filter.indexOf(item.id) !== -1;
}
}

$scope.loadNewFilter = function (){
$scope.filter = [-1];
$scope.$apply();
}

});

最佳答案

我想这就是你想要的:

var app = angular.module('myApp', []);    
app.controller('myCtrl', function ($scope) {
$scope.values = [
{id: 1},
{id: 2},
{id: 3},
{id: 4},
{id: 5},
{id: 6}];

$scope.filter = [1,2,3,4,5,6];

$scope.filterIds = function (ids) {
return function (item) {
var filter = $scope.filter;
return filter.indexOf(item.id) !== -1;
}
}

$scope.loadNewFilter = function (){
$scope.filter = [1,5];
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div data-ng-controller="myCtrl">
<ul>
<li data-ng-repeat="item in testValue=(values | filter:filterIds())">
<code>#{{item.id}}</code> Item
</li>
</ul>
<p ng-show="!testValue.length">no vals with this filter</p>
<button ng-click="loadNewFilter()"> filter now</button>
</div>
</div>

FIDDLE LINK

This is Another one FIDDLE LINK check this also

关于javascript - Angular js - 如果 ng-repeat 为空则显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25866121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com