我有一个非常简单的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; } }I have a pretty simple if statement but I don't use javascript too much so I think I have made an error somewhere. If you go to my page you can see the value gets alerted as undefined, but a block of code still gets skipped even though the if parameters are == undefined. Does it matter that this is an AngularJS app?
web page: 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)) {To check if ($scope.hoursAgo === undefined || $scope.hoursAgo == NaN)
write like this
if ($scope.hoursAgo === 'undefined' || isNaN($scope.hoursAgo)) {更多推荐
发布评论