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

B.Balamanigandan picture B.Balamanigandan · Dec 8, 2015 · Viewed 7.2k times · Source

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

Answer

AntiHeadshot picture AntiHeadshot · Dec 8, 2015

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

plnkr