javascript - 如果在过滤器中返回空值,如何显示 'No Books Found' 行?

标签 javascript angularjs ng-show angularjs-ng-show

我正在尝试显示“未找到书籍!”如果 Angular 过滤器在 AngularJs 中没有返回结果,则行。

但我无法以某种方式让它工作。这是我的 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 将在应用过滤器后返回数组的 length

Fiddle

关于javascript - 如果在过滤器中返回空值,如何显示 'No Books Found' 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35765498/

相关文章:

angularjs - 有条件地应用评估的表达式类名?

javascript - 如何为音频文件数组的每个索引动态地将 src 值绑定(bind)到指令范围变量

angularjs - 如果没有隐藏,则为输入元素设置必需的属性

javascript - Angular 和 ng-show 不禁用项目

javascript - ReactJS - 通过 onclick 切换类

javascript - 为什么 event.preventDefault() 无法取消 Angular js 中的 $routeChangeStart 事件

javascript - 尝试/捕获不捕获快速异步功能中的所有错误?

AngularJS:ng-show/ng-hide 不适用于 `{{ }}` 插值

javascript - JQuery 每个循环在查看其余代码之前都会循环遍历一行代码 x 次

javascript - 迭代数组,为每个元素调用 $.ajax。返回包含所有 ajax 结果的数组