我有一个非常简单的if语句,但是我没有太多使用javascript,所以我认为我在某个地方出错了。如果转到我的页面,您会看到该值被警告为未定义,但是即使if参数为== undefined,代码块仍会被跳过。这是AngularJS应用程序有关系吗?

网页:http://alainwebdesign.ca/pl2/#/petType

javascript:

$scope.setDate = function (dateSelected) {
    alert(dateSelected);
    var now = new Date();
    $scope.latest = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes());
    $scope.hoursAgo = Math.round((($scope.latest - dateSelected) / 3600000) * 100) / 100;
    if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN) {
        alert("Please choose a date/time");
    }
    else {
        alert('You lost your pet ' + $scope.hoursAgo + ' hour(s) ago');
        $scope.checkDateSet = true;
    }
}

最佳答案

检查if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN)

这样写

if ($scope.hoursAgo === 'undefined' || isNaN($scope.hoursAgo)) {

关于javascript - 比较日期与NaN和未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39976077/

10-12 00:22
查看更多