How to display placeholders in AngularJS for undefined expression values?
Asked Answered
S

5

61

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.

Sachasachem answered 27/3, 2013 at 12:14 Comment(0)
A
96

{{ 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>
Alonzo answered 27/3, 2013 at 15:2 Comment(2)
FWIW, make sure to put the default value before any filters (e.g. {{ counter || '?' | myfilter }}).Conjoin
This answer is simple and clean, but the drawback is that it will not only display the placeholder on null and undefined, but also on 0, which can be a valid value.Myrtismyrtle
S
25

I would do it like this, but maybe there is a better way:

angular.module('app').filter('placeholdEmpty', function(){
  return function(input){
    if(!(input == undefined || input == null)){
      return input;
    } else {
      return "placeholder";
    }
  }
});

and then use {{ x | placeholdEmpty}}

Sachasachem answered 27/3, 2013 at 12:14 Comment(5)
The advantage of the filter solution over {{ counter || '?'}} is that you can distinguish between undefined, null or zero.Distichous
Another important advantage of placeholder filter is that in future, you can easily change the placeholder text, if you decide that you dont like '?'...Anklet
And how do you apply a filter to x?Festatus
@JustGoscha ..Works Perfect..ThanksUncovered
This filter doesn't allow to provide the default without modifying the filter. Look at @naXa answer below.Harebrained
L
13

I do it with ng-show, like this:

<strong>{{username}}</strong>
<span class="empty" ng-show="!username">N/A</span>

Sure, it adds a lot more elements to my view that I might be able to handle differently. I like though how easy it is to clearly see where my placeholder/empty values are, and I can style them differently as well.

Language answered 27/3, 2013 at 13:24 Comment(0)
O
5

Implement default filter:

app.filter('default', function(){
  return function(value, def) {
    return (value === undefined || value === null? def : value);
  };
});

And use it as:

{{ x | default: '?' }}

The advantage of the filter solution over {{ x || '?'}} is that you can distinguish between undefined, null or 0.

Outofdate answered 4/3, 2016 at 3:12 Comment(0)
C
2

Implementing default-ish filters works, but if you're using only numbers you can use angular's own number filter

If the input is null or undefined, it will just be returned. If the input is infinite (Infinity or -Infinity), the Infinity symbol '∞' or '-∞' is returned, respectively. If the input is not a number an empty string is returned.

{{ (val | number ) || "Number not provided"}}
Courier answered 28/7, 2016 at 22:0 Comment(1)
Thanks! This works with ng-bind="(val | number) || 'Default'" as well!Harebrained

© 2022 - 2024 — McMap. All rights reserved.