Adr*_*wan 96 html javascript angularjs
我希望有一个占位符,例如<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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
jsFiddle:http://jsfiddle.net/adrn/PEumV/1/
谢谢!
Mar*_*cok 252
对方法的调整只需要您指定一次过滤器:
<li ng-repeat="bar in filteredBars = (bars | filter:barFilter)">{{bar.name}}</li>
</ul>
<p ng-hide="filteredBars.length">Nothing here!</p>
Run Code Online (Sandbox Code Playgroud)
Adr*_*wan 37
这是使用ng-show的技巧
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>
<p ng-show="(bars | filter:barFilter).length == 0">Nothing here!</p>
</div>
Run Code Online (Sandbox Code Playgroud)
jsFiddle: http ://jsfiddle.net/adrn/PEumV/2/
cai*_*ci2 21
摘自这份官方文件,他们是如何做到的:
ng-repeat="friend in friends | filter:q as results"
Run Code Online (Sandbox Code Playgroud)
然后将结果用作数组
<li class="animate-repeat" ng-if="results.length == 0">
<strong>No results found...</strong>
</li>
Run Code Online (Sandbox Code Playgroud)
完整片段:
<div ng-controller="repeatController">
I have {{friends.length}} friends. They are:
<input type="search" ng-model="q" placeholder="filter friends..." aria-label="filter friends" />
<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>
<li class="animate-repeat" ng-if="results.length == 0">
<strong>No results found...</strong>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55288 次 |
最近记录: |