Angularjs Template Default Value if Binding Null / Undefined (With Filter)

JavascriptTemplatesBindingAngularjsDefault

Javascript Problem Overview


I have a template binding that displays a model attribute called 'date' which is a date, using Angular's date filter.

<span class="gallery-date">{{gallery.date | date:'mediumDate'}}</span>

So far so good. However at the moment, if there is no value in the date field, the binding displays nothing. However, I would like it to display the string 'Various' if there is no date.

I can get the basic logic using a binary operator:

<span class="gallery-date">{{gallery.date || 'Various'}}</span>

However I can't get it to work with the date filter:

<span class="gallery-date">{{gallery.date | date:'mediumDate' || "Various"}}</span>

How can I use the binary operator alongside the date filter?

Javascript Solutions


Solution 1 - Javascript

Turns out all I needed to do was wrap the left-hand side of the expression in soft brackets:

<span class="gallery-date">{{(gallery.date | date:'mediumDate') || "Various"}}</span>

Solution 2 - Javascript

I made the following filter:

angular.module('app').filter('ifEmpty', function() {
    return function(input, defaultValue) {
        if (angular.isUndefined(input) || input === null || input === '') {
            return defaultValue;
        }

        return input;
    }
});

To be used like this:

<span>{{aPrice | currency | ifEmpty:'N/A'}}</span>
<span>{{aNum | number:3 | ifEmpty:0}}</span>

Solution 3 - Javascript

Just in case you want to try something else. This is what worked for me:

Based on Ternary Operator which has following structure:

condition ? value-if-true : value-if-false

As result:

{{gallery.date?(gallery.date | date:'mediumDate'):"Various" }}

Solution 4 - Javascript

How can I use the binary operator alongside the date filter?

<span class="gallery-date">{{gallery.date | date:'mediumDate' || "Date Empty"}}</span>

you also try:

<span class="gallery-date">{{ gallery.date == 'NULL' ? 'mediumDate' : "gallery.date"}}</span>

Solution 5 - Javascript

I really liked this answer, with ngBind, your default text can just live in the element body, and then if the ngBind evaluates to something non-null/undefined, your content is replaced automatically, and everythings happy

https://stackoverflow.com/questions/13291856/angularjs-setting-default-values-to-display-before-evaluation

Solution 6 - Javascript

In your cshtml,

<tr ng-repeat="value in Results">                
 <td>{{value.FileReceivedOn | mydate | date : 'dd-MM-yyyy'}} </td>
</tr>

In Your JS File, maybe app.js,

Outside of app.controller, add the below filter.

Here the "mydate" is the function which you are calling for parsing the date. Here the "app" is the variable which contains the angular.module

app.filter("mydate", function () {
    var re = /\/Date\(([0-9]*)\)\//;
    return function (x) {
        var m = x.match(re);
        if (m) return new Date(parseInt(m[1]));
        else return null;
    };
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUndistractionView Question on Stackoverflow
Solution 1 - JavascriptUndistractionView Answer on Stackoverflow
Solution 2 - JavascriptSupergibbsView Answer on Stackoverflow
Solution 3 - JavascriptPolView Answer on Stackoverflow
Solution 4 - JavascriptRizwanView Answer on Stackoverflow
Solution 5 - JavascriptchrismarxView Answer on Stackoverflow
Solution 6 - JavascriptMohamedasiqView Answer on Stackoverflow