How to display placeholders in AngularJS for undefined expression values?

JustGoscha picture JustGoscha · Mar 27, 2013 · Viewed 44.4k times · Source

If I have an expression {{ x }} and x is undefined or null, then how can I display a placeholder for it?

I provided one solution in my answer, but I would like to know what other ways there are. Maybe, also for placeholder for promises.

Answer

Umur Kontacı picture Umur Kontacı · Mar 27, 2013

{{ counter || '?'}}. Just pure javascript. || can be used as default value. Since it would be different empty messages in each, a generalized directive would not be suitable for many cases.

If you want to apply a different class to empty ones, that's also built-in:

<div ng-class="{empty: !counter}" ng-bind="counter || ?"></div>