angularjs – 当值为空时如何隐藏标签文本?

前端之家收集整理的这篇文章主要介绍了angularjs – 当值为空时如何隐藏标签文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在表单元格中,我列出了使用ng-repeat填充的几个项目,使用下面的结构.但是,对于某些条目,诸如“user.favcolor”之类的属性是空白的.在这种情况下,最简单的方法是隐藏“喜欢的颜色:”这样的文字,这样我就不会得到一个“喜欢的颜色:”和没有价值的行?
<table>
            <thead>
                <tr>
                    <th>Price</th>
                    <th>Plan Contents</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="tip in tips">
                    <td>{{tip.priceMonthly}}</td>
                    <td><span>Name: {{user.name}}</span>
                        <span>ID: {{user.id}}</span>
                        <span>Favorite color: {{user.favcolor}}</span>
                    </td>
                </tr>
            </tbody>
        </table>
您可以使用 ng-show指令:
<span ng-show="user.favcolor">Favorite color: {{user.favcolor}}</span>

ng-show工作,使得仅当表达式的计算结果为true时才显示该元素.这里的一个空字符串将评估为假隐藏整个元素.

或者,您也可以指定默认值:

<span>Favorite color: {{user.favcolor || "Not specified" }}</span>

在这种情况下,如果user.favcolor计算结果为false,那么将打印未指定.

原文链接:https://www.f2er.com/angularjs/142779.html

猜你在找的Angularjs相关文章