我想要一个占位符,例如<No result>当过滤器结果返回空时。谁能帮忙吗?我什至不知道从哪里开始…
<No result>
HTML :
<div ng-controller="Ctrl"> <h1>My Foo</h1> <ul> <li ng-repeat="foo in foos"> <a href="#" ng-click="setBarFilter(foo.name)">{{foo.name}}</a> </li> </ul> <br /> <h1>My Bar</h1> <ul> <li ng-repeat="bar in bars | filter:barFilter">{{bar.name}}</li> </ul> </div>
JS :
function Ctrl($scope) { $scope.foos = [{ name: 'Foo 1' },{ name: 'Foo 2' },{ name: 'Foo 3' }]; $scope.bars = [{ name: 'Bar 1', foo: 'Foo 1' },{ name: 'Bar 2', foo: 'Foo 2' }]; $scope.setBarFilter = function(foo_name) { $scope.barFilter = {}; $scope.barFilter.foo = foo_name; } }
jsFiddle :http : //jsfiddle.net/adrn/PEumV/1/
谢谢!
对只需要指定一次过滤器的方法进行了调整:
<li ng-repeat="bar in filteredBars = (bars | filter:barFilter)">{{bar.name}}</li> </ul> <p ng-hide="filteredBars.length">Nothing here!</p>
小提琴