如果在过滤器中返回空值,如何显示 'No Books Found' 行?
How can I show the 'No Books Found' row if null value is returned in filter?
如果 angular 过滤器 returns 在 AngularJs 中没有结果,我将尝试显示 'No Books Found!' 行。
但我无法以某种方式让它工作。这是我的 html 代码:
<div class='col-md-6'>
<div class='row'>
<div class="col-md-12">
<div class="input-group">
<input type='text' ng-model='search' class='form-control' aria-describedby="basic-addon2">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><i class='glyphicon glyphicon-search'></i></button>
</span>
</div>
</div>
</div>
<div class='row' id='search-table'>
<div class="col-md-12">
<table class='table table-bordered table-hover table-collapse' ng-init="books = [{
title : 'The Life-Changing Magic of Tidying Up: The Japanese Art of Decluttering and Organizing',
author : 'Marie Kondo',
price : '9.68'
},
{
title : 'Quieting Your Heart: 6-Month Bible-Study Journal',
author : 'Darlene Schacht',
price : '7.14'
},
{
title : 'First 100 Words',
author : 'Roger Priddy',
price : '3.19'
}]">
<thead>
<th> TITLE </th>
<th> AUTHOR </th>
<th> PRICE </th>
</thead>
<tbody>
<tr ng-repeat="book in books | filter:search">
<td> {{ book.title }} </td>
<td> {{ book.author }} </td>
<td> {{ book.price }} </td>
</tr>
<tr class='info' ng-show="!books.length">
<td colspan=3> <i class='glyphicon glyphicon-info-sign '></i> No Books Found </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
我错过了什么吗?提前致谢!
Use (books|filter:search).length
as books.length
will always return length of the base
array which is not being affected by filter
(books|filter:search).length
将 return 应用过滤器后数组的 length
。
如果 angular 过滤器 returns 在 AngularJs 中没有结果,我将尝试显示 'No Books Found!' 行。
但我无法以某种方式让它工作。这是我的 html 代码:
<div class='col-md-6'>
<div class='row'>
<div class="col-md-12">
<div class="input-group">
<input type='text' ng-model='search' class='form-control' aria-describedby="basic-addon2">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><i class='glyphicon glyphicon-search'></i></button>
</span>
</div>
</div>
</div>
<div class='row' id='search-table'>
<div class="col-md-12">
<table class='table table-bordered table-hover table-collapse' ng-init="books = [{
title : 'The Life-Changing Magic of Tidying Up: The Japanese Art of Decluttering and Organizing',
author : 'Marie Kondo',
price : '9.68'
},
{
title : 'Quieting Your Heart: 6-Month Bible-Study Journal',
author : 'Darlene Schacht',
price : '7.14'
},
{
title : 'First 100 Words',
author : 'Roger Priddy',
price : '3.19'
}]">
<thead>
<th> TITLE </th>
<th> AUTHOR </th>
<th> PRICE </th>
</thead>
<tbody>
<tr ng-repeat="book in books | filter:search">
<td> {{ book.title }} </td>
<td> {{ book.author }} </td>
<td> {{ book.price }} </td>
</tr>
<tr class='info' ng-show="!books.length">
<td colspan=3> <i class='glyphicon glyphicon-info-sign '></i> No Books Found </td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
我错过了什么吗?提前致谢!
Use
(books|filter:search).length
asbooks.length
will always return length of thebase
array which is not being affected byfilter
(books|filter:search).length
将 return 应用过滤器后数组的 length
。