我有两个使用ng-repeat的东西列表。然后我想要它说"空"如果列表中没有项目。
这是更新版本:
<!-- First List -->
<div ng-repeat="request in requests" ng-if="request.condition1 == 'something'">
{{request}}
</div>
<div>Empty list</div>
<!-- Second List (both use requests array but have different conditions-->
<div ng-repeat="request in requests" ng-if="request.condition22222 == 'something else'">
{{request}}
</div>
<div>Empty list</div>
答案 0 :(得分:1)
试试这个:
(function(angular) {
'use strict';
angular.module('app', [])
.filter('decorate', function() {
return function(input, model) {
return input.filter(function(val){
return val[model.prop] == model.val;
});
};
}).controller('MyController', ['$scope', '$filter', function($scope, $filter) {
$scope.requests = [{name:'Tom', age:23},{name:'Henry', age:23},{name:'Max', age:33}];
$scope.$filter = $filter;
}])
})(window.angular);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<div ng-controller="MyController">
<div ng-init='items1=$filter("decorate")(requests, {prop:"age", val:23})'>First list(age=23):</div>
<div ng-repeat='item in items1' ng-if='items1.length > 0' >{{item | json}}</div>
<div ng-if='items1.length == 0'>Empty</div>
<br/>
<div ng-init='items2=$filter("decorate")(requests, {prop:"name", val:"Sam"})'>Second list(name=Sam):</div>
<div ng-repeat='item in items2' ng-if='items2'>{{item | json}}</div>
<div ng-if='items2.length == 0'>Empty</div>
</div>
</body>
&#13;
答案 1 :(得分:-1)