【问题标题】:How to ngShow a div when filtered search bar returns nothing?过滤后的搜索栏不返回任何内容时如何显示 div?
【发布时间】:2015-05-07 19:12:57
【问题描述】:

首先我将提供背景信息,然后显示一些代码 sn-ps。

  1. 我有一个 ng-repeat,它重复盒子状的小部件,里面有数据/信息。

  2. 我有一个搜索栏,用于过滤小部件中的数据,并且仅在搜索栏中显示包含该字符串的小部件。

  3. 考虑到数字 2,当输入的字符串不在任何小部件中时,它会使页面空白且没有小部件。 (它使没有该字符串的小部件消失)

我想要的是:当没有返回结果而不是空白页时显示一个 div。

为了举例,我有:

<input type = "text" data-ng-model = "searchWidgets" />

<div id="main-content-body" data-ng-repeat="widget in widgets | filter:searchWidgets">

    <div id="falsy-search" ng-show="!searchWidgets.length">
        <h1 id="inset-message" class="text-center">No Results Found</h1>
    </div>

    <section>
        (this is where all the widgets get displayed)
    </section>

</div>

提前感谢您的时间和投入!

【问题讨论】:

    标签: javascript angularjs web


    【解决方案1】:

    你可以有几种选择,最简单的一种是将过滤的Widgets保存为自己的对象,然后使用ng-show:

    <div id="main-content-body" data-ng-repeat="widget in (filteredWidgets = (widgets | filter:searchWidgets))">
    
        <div id="falsy-search" ng-show="!searchWidgets.length || filteredWidgets.length == 0">
            <h1 id="inset-message" class="text-center">No Results Found</h1>
        </div>
    
        <section>
            (this is where all the widgets get displayed)
        </section>
    
    </div>
    

    Related SO Question

    【讨论】:

    • 我稍微改变了 ng-show。我有&lt;div ng-show="!searchWidgets.length == 0|| filteredWidgets.length == 0"&gt;。现在,每当我搜索任何内容或搜索不正确时,它都会显示 div。
    • @Kepler 我的第一个猜测是“!searchWidgets.length == 0”。我相信!是额外的(以及此问题的原因)
    • 再次更正,你这个天才!但是,当我现在将字符串退格到搜索栏时,div 仍然存在。谢谢你让我用这个来打扰你:)
    • 那可能是因为 searchWidgets.length 为 0。我想,我的下一个问题是,“你为什么要把它作为 ng-show 的一部分?”。在我看来,这个 div 应该只取决于结果。
    • 如果不存在,则只要搜索栏中有内容,就会出现 div。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多