0

Say I have the following HTML:

<p>{{ booleanVariable ? "The variable is true!" : null }}</p>

If booleanVariable is false, the expression should be empty and the <p></p> shouldn't render. However, it just prints the "null" into the <p> tag, as if I'd just typed <p>null</p>.

How would I get the expression to just render nothing?

3 Answers 3

2

Use undefined instead of null.

<p>{{ booleanVariable ? "The variable is true!" : undefined }}</p>

A simpler solution would be ngShow:

<p ng-show="booleanVariable">The variable is true!</p>
Sign up to request clarification or add additional context in comments.

1 Comment

Using ng-show your element will be hidden which can be unwanted. (Example: element with css' paddings -- will break the layout). On the other hand, instead of null you can use "" double quotes.
1

Empty string is make sense.

<p>{{ booleanVariable ? "The variable is true!" : '' }}</p>

Comments

0

The best solution would be to use

<p ng-if="booleanVariable">The variable is true!</p>

ng-show will have <p> in DOM but its just hidden if you use ng-if <p> will not be in DOM also

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.