我有一个带有日期选择器的简单角形。我正在拨打服务以获取所有公共假期列表。如何检查日期选择器中的选定日期是否在该列表中。如果它在该列表中显示警告“选定日期是公共假日”我也想排除周末。
<input type="date" name="startDate" ng-model="startDate" required>
Angular Function,用于检索所有公共假期的列表。 response.data
有三个字段ID,Description和HolidayDate
function publicHolidays() {
holidayService.GetPublicHolidays().then(function (response) {
$scope.holidays = response.data;
})
}
如何查看我的开始日期是否在此公众假期列表中,如果是,则显示警告。另外我怎么能排除周末
response.data
已
答案 0 :(得分:0)
我想我不确定你的意思&#34;排除周末&#34;但是,每次选择的日期发生变化时,如何检查$scope.startDate
是周末还是假日。
<input type="date" name="startDate" ng-model="startDate" ng-change"checkHolidaysAndWeekends()" required>
// Controller
$scope.checkHolidaysAndWeekends = function() {
if($scope.startDate.getDay() == 6 || $scope.startDate.getDay() == 0)
{ alert('Weekend!') };
angular.forEach($scope.holidays, function(holiday, key){
if ($scope.startDate == holiday.holidayDate)
{ alert("That's a holiday") };
});
};
答案 1 :(得分:0)
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp',[])
.controller('MyCtrl', function MyCtrl($scope) {
$scope.holidays =[
{
description: 'description1',
holidayDate : "1999-03-03T00:00:00",
id:1
},
{
description: 'description2',
holidayDate : "2017-03-03T00:00:00",
id:2
},
{
description: 'description3',
holidayDate : "2016-03-03T00:00:00",
id:3
},
{
description: 'description4',
holidayDate : "2015-03-03T00:00:00",
id:4
},
{
description: 'description5',
holidayDate : "2014-03-03T00:00:00",
id:5
}
];
$scope.dateChanged = function (startDate) {
startDate = startDate.setHours(0,0,0,0);
startDate = new Date(startDate);
angular.forEach($scope.holidays, function (data) {
var holidayDate = new Date(data.holidayDate);
holidayDate = holidayDate.setHours(0,0,0,0);
holidayDate = new Date(holidayDate);
var holidayDateTime = holidayDate.getTime();
var startDateTime = startDate.getTime();
if (holidayDateTime == startDateTime) {
alert('this is holiday');
}
})
}
})
</script>
</head>
<body ng-controller="MyCtrl">
<input type="date" name="startDate" ng-model="startDate" ng-change="dateChanged(startDate)" required>
</body>
</html>