我有一个列表,附有一些过滤器。我想计算这些项目,如果有0个项目显示消息......
<li ng-repeat="item in items = (items | sortWithTab:tab | filter:search")>
{{ item.name }}
</li>
<div ng-if="items.length === 0">No items</div>
项目集包含一个带有名称的tab
字段,我有相关按钮可按名称进行过滤,这会更新$scope.tab
名称。
如果我想输出该列表中的项目数,我会items.length
,这是有效的。但是,如果我尝试返回已经按下的标签,则不会更新。有谁知道为什么?
以下示例:
var app = angular.module('App', []);
app.controller('SampleCtrl', ['$scope',
function($scope) {
$scope.tab = 'all';
$scope.changeTab = function(tab)
{
$scope.tab = tab;
}
$scope.items = [
{name: 'One', tab: 'live'},
{name: 'Two', tab: 'today'},
{name: 'Three', tab: 'today'},
{name: 'Four', tab: 'today'},
{name: 'Five', tab: 'live'},
{name: 'Six', tab: 'today'},
{name: 'Seven', tab: 'today'},
{name: 'Eight', tab: 'live'}
];
}]);
app.filter('sortWithTab', function() {
return function(list, tab) {
var filtered;
var i;
if (tab == 'all') {
return list;
} else if (tab == 'today') {
filtered = [];
for (i = 0; i < list.length; i++) {
if(list[i].tab == 'today')
filtered.push(list[i]);
}
return filtered;
} else if (tab == 'live') {
filtered = [];
for (i = 0; i < list.length; i++) {
if(list[i].tab == 'live')
filtered.push(list[i]);
}
return filtered;
} else {
return list;
}
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="SampleCtrl">
<a ng-click="changeTab('all')">All</a>
<a ng-click="changeTab('today')">Today</a>
<a ng-click="changeTab('live')">Live</a>
<ul>
<li ng-repeat="item in items = (items | sortWithTab:tab)">
{{ item.name }}
</li>
Total: {{ items.length }}
</ul>
</div>
&#13;
答案 0 :(得分:1)
更改
<li ng-repeat="item in items = (items | sortWithTab:tab)">
到
<li ng-repeat="item in filtered = (items | sortWithTab:tab)">
var app = angular.module('App', []);
app.controller('SampleCtrl', ['$scope',
function($scope) {
$scope.tab = 'all';
$scope.changeTab = function(tab) {
$scope.tab = tab;
}
$scope.items = [{
name: 'One',
tab: 'live'
}, {
name: 'Two',
tab: 'today'
}, {
name: 'Three',
tab: 'today'
}, {
name: 'Four',
tab: 'today'
}, {
name: 'Five',
tab: 'live'
}, {
name: 'Six',
tab: 'today'
}, {
name: 'Seven',
tab: 'today'
}, {
name: 'Eight',
tab: 'live'
}];
}
]);
app.filter('sortWithTab', function() {
return function(list, tab) {
var filtered;
var i;
if (tab == 'all') {
return list;
} else if (tab == 'today') {
filtered = [];
for (i = 0; i < list.length; i++) {
if (list[i].tab == 'today')
filtered.push(list[i]);
}
return filtered;
} else if (tab == 'live') {
filtered = [];
for (i = 0; i < list.length; i++) {
if (list[i].tab == 'live')
filtered.push(list[i]);
}
return filtered;
} else {
return list;
}
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="SampleCtrl">
<a ng-click="changeTab('all')">All</a> |
<a ng-click="changeTab('today')">Today</a> |
<a ng-click="changeTab('live')">Live</a> |
<ul>
<li ng-repeat="item in filtered = (items | sortWithTab:tab)">
{{ item.name }}
</li>
<div ng-if="filtered.length === 0">No items</div>
Total: {{ filtered.length }}
</ul>
</div>
&#13;