3

how to Implement to check for null Date in angularJS using ng-if ?

The HTML Source Code is

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>D.O.B</th>
        </tr>
    </thead>
  <tr ng-repeat="x in names>
    <td>{{ x.Name }}</td>
    <td><span ng-if="x.DOB != null">{{ formatDate(x.DOB) |  date:'dd-MM-yyyy' }}</span></td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {

    $scope.names = [
        { Name: 'Jani', DOB: '' },
        { Name: 'Hege', DOB: '1995-05-07' },
        { Name: 'Kai', DOB: '1995-08-24' }
    ];

    $scope.formatDate = function (date) {
        return new Date(date);
    };

});
</script>

</body>
</html>

Here I wish to eliminate the Date Value is equal to null or NaN-NaN-0NaN using angularJS ng-if

In the above Source code is not performing the IF Condition

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130

2 Answers2

5

You can use ng-if="x.DOB" because an empty string or undefined will return false

plnkr

AntiHeadshot
  • 1,130
  • 9
  • 24
1

Try using this:

<td>
 <span ng-if="x.DOB != ''">{{ formatDate(x.DOB) |  date:'dd-MM-yyyy' }}
</span>
</td>

DOB = " "; means that the empty String is assigned to DOB and DOB = null; means that (null) or "no value at all" is assigned to DOB

UPDATE:

<span ng-if="x.DOB">{{ formatDate(x.DOB) |  date:'dd-MM-yyyy' }}
    </span>
Ved
  • 11,837
  • 5
  • 42
  • 60