Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the label text when the value is empty?

Tags:

angularjs

Within a table cell, I am listing several items that are populated using ng-repeat, using the structure below. However, for some entries, properties such as "user.favcolor" are blank. What's the easiest way to hide text such as "Favorite color:" in that case so that I don't end up with a row that has "Favorite color:" and no value beside it?

        <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>
like image 424
user2793297 Avatar asked Sep 24 '13 23:09

user2793297


People also ask

How do you hide a label?

The hidden attribute hides the <label> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <label> element is not visible, but it maintains its position on the page.

How do you hide label tags in CSS?

Hiding the label with display: none; is bad for web accesibility and you shouldn't do it. Try visibility: hidden; instead. yep, why I added a label I didn't want to see in the first place. display: block; height: 0; visibility: hidden; .


1 Answers

You can use the ng-show directive for this:

<span ng-show="user.favcolor">Favorite color: {{user.favcolor}}</span>

The ng-show works such that the element is only shown if the expression evaluates to true. An empty string here will evaluate to false hiding the entire element.

Alternatively, you can also specify a default value:

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

In this case, if user.favcolor evaluates to a false, it will print Not specified instead.

like image 112
xbonez Avatar answered Nov 03 '22 09:11

xbonez