我在Angular中有一个调用我的WebApi GET服务的get动作。然后我使用结果填充详细信息表单或编辑表单。如果我正在填充编辑表单,那么我需要清理日期,以便HTML 5日期选择器将从响应数据中显示正确的日期。
这是我的Angular get方法:
$scope.getAction = function (actionId, populateObject) {
$http.get(actionUrl + '/' + actionId)
.then(function (response) {
// Test front end exception message;
// throw "test exception";
switch (populateObject) {
case "details":
$scope.data.actionDetails = response.data;
break;
case "edit":
// populate editAction Object
$scope.data.editAction = response.data;
// sanitize dates for action type.
if (response.data.actionTypeId == 1) {
//var actionEffectiveDate = undefined; // if you get null back from database, you'll keep this undefined
//if (response.data.actionType1.actionEffectiveDate !== null) {
// // only make an actual date if there is something stored
// var actionEffectiveDate = new Date(response.data.actionType1.actionEffectiveDate);
//}
//$scope.sanitizedActionEffectiveDate = $filter('date')(actionEffectiveDate, "yyyy-MM-dd")
$scope.sanitizedActionEffectiveDate = $scope.sanitizeDate(response.data.actionType1.actionEffectiveDate);
}
if (response.data.actionTypeId == 2) {
var actionEffectiveDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.startDate !== null) {
// only make an actual date if there is something stored
var startDate = new Date(response.data.actionType2.startDate);
}
$scope.sanitizedStartDate = $filter('date')(startDate, "yyyy-MM-dd")
var endDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.endDate !== null) {
// only make an actual date if there is something stored
var endDate = new Date(response.data.actionType2.endDate);
}
$scope.sanitizedEndDate = $filter('date')(endDate, "yyyy-MM-dd")
var first30DayExtensionInvestigativeLeaveStartDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.first30DayExtensionInvestigativeLeaveStartDate !== null) {
// only make an actual date if there is something stored
var first30DayExtensionInvestigativeLeaveStartDate = new Date(response.data.actionType2.first30DayExtensionInvestigativeLeaveStartDate);
}
$scope.sanitizedFirst30DayExtensionInvestigativeLeaveStartDate = $filter('date')(first30DayExtensionInvestigativeLeaveStartDate, "yyyy-MM-dd")
var first30DayExtensionInvestigativeLeaveEndDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.first30DayExtensionInvestigativeLeaveEndDate !== null) {
// only make an actual date if there is something stored
var first30DayExtensionInvestigativeLeaveEndDate = new Date(response.data.actionType2.first30DayExtensionInvestigativeLeaveEndDate);
}
$scope.sanitizedFirst30DayExtensionInvestigativeLeaveEndDate = $filter('date')(first30DayExtensionInvestigativeLeaveEndDate, "yyyy-MM-dd")
var chcoApproved1st30DayInvestigativeLeave = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.chcoApproved1st30DayInvestigativeLeave !== null) {
// only make an actual date if there is something stored
var chcoApproved1st30DayInvestigativeLeave = new Date(response.data.actionType2.chcoApproved1st30DayInvestigativeLeave);
}
$scope.sanitizedChcoApproved1st30DayInvestigativeLeave = $filter('date')(chcoApproved1st30DayInvestigativeLeave, "yyyy-MM-dd")
var chcoApproved2nd30DayExtensionInvestigativeLeave = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.chcoApproved2nd30DayExtensionInvestigativeLeave !== null) {
// only make an actual date if there is something stored
var chcoApproved2nd30DayExtensionInvestigativeLeave = new Date(response.data.actionType2.chcoApproved2nd30DayExtensionInvestigativeLeave);
}
$scope.sanitizedChcoApproved2nd30DayExtensionInvestigativeLeave = $filter('date')(chcoApproved2nd30DayExtensionInvestigativeLeave, "yyyy-MM-dd")
var chcoApproved3rd30DayExtensionInvestigativeLeave = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.chcoApproved3rd30DayExtensionInvestigativeLeave !== null) {
// only make an actual date if there is something stored
var chcoApproved3rd30DayExtensionInvestigativeLeave = new Date(response.data.actionType2.chcoApproved3rd30DayExtensionInvestigativeLeave);
}
$scope.sanitizedChcoApproved3rd30DayExtensionInvestigativeLeave = $filter('date')(chcoApproved3rd30DayExtensionInvestigativeLeave, "yyyy-MM-dd")
var second30DayExtensionInvestigativeLeaveStartDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.second30DayExtensionInvestigativeLeaveStartDate !== null) {
// only make an actual date if there is something stored
var second30DayExtensionInvestigativeLeaveStartDate = new Date(response.data.actionType2.second30DayExtensionInvestigativeLeaveStartDate);
}
$scope.sanitizedSecond30DayExtensionInvestigativeLeaveStartDate = $filter('date')(second30DayExtensionInvestigativeLeaveStartDate, "yyyy-MM-dd")
var Second30DayExtensionInvestigativeLeaveEndDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.second30DayExtensionInvestigativeLeaveEndDate !== null) {
// only make an actual date if there is something stored
var second30DayExtensionInvestigativeLeaveEndDate = new Date(response.data.actionType2.second30DayExtensionInvestigativeLeaveEndDate);
}
$scope.sanitizedSecond30DayExtensionInvestigativeLeaveEndDate = $filter('date')(second30DayExtensionInvestigativeLeaveEndDate, "yyyy-MM-dd")
var third30DayExtensionInvestigativeLeaveStartDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.third30DayExtensionInvestigativeLeaveStartDate !== null) {
// only make an actual date if there is something stored
var third30DayExtensionInvestigativeLeaveStartDate = new Date(response.data.actionType2.third30DayExtensionInvestigativeLeaveStartDate);
}
$scope.sanitizedThird30DayExtensionInvestigativeLeaveStartDate = $filter('date')(third30DayExtensionInvestigativeLeaveStartDate, "yyyy-MM-dd")
var third30DayExtensionInvestigativeLeaveEndDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.third30DayExtensionInvestigativeLeaveEndDate !== null) {
// only make an actual date if there is something stored
var third30DayExtensionInvestigativeLeaveEndDate = new Date(response.data.actionType2.third30DayExtensionInvestigativeLeaveEndDate);
}
$scope.sanitizedThird30DayExtensionInvestigativeLeaveEndDate = $filter('date')(third30DayExtensionInvestigativeLeaveEndDate, "yyyy-MM-dd")
var noticeLeaveStartDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.noticeLeaveStartDate !== null) {
// only make an actual date if there is something stored
var noticeLeaveStartDate = new Date(response.data.actionType2.noticeLeaveStartDate);
}
$scope.sanitizedNoticeLeaveStartDate = $filter('date')(noticeLeaveStartDate, "yyyy-MM-dd")
var noticeLeaveEndDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType2.noticeLeaveEndDate !== null) {
// only make an actual date if there is something stored
var noticeLeaveEndDate = new Date(response.data.actionType2.noticeLeaveEndDate);
}
$scope.sanitizedNoticeLeaveEndDate = $filter('date')(noticeLeaveEndDate, "yyyy-MM-dd")
}
if (response.data.actionTypeId == 17) {
var responseDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType17.responseDate !== null) {
// only make an actual date if there is something stored
var responseDate = new Date(response.data.actionType17.responseDate);
}
$scope.sanitizedResponseDate = $filter('date')(responseDate, "yyyy-MM-dd")
var requestReceivedDate = undefined; // if you get null back from database, you'll keep this undefined
if (response.data.actionType17.requestReceivedDate !== null) {
// only make an actual date if there is something stored
var requestReceivedDate = new Date(response.data.actionType17.requestReceivedDate);
}
$scope.sanitizedRequestReceivedDate = $filter('date')(responseDate, "yyyy-MM-dd")
}
break;
}
})
.catch(function (error) {
$scope.data.actionDetailsError = error;
});
}
所以你可以看到我必须一遍又一遍地编写相同的代码来清理每个日期。 但是在第一个上你可以看到那部分注释了我试图调用我正在处理的通用行为的位置。
//var actionEffectiveDate = undefined; // if you get null back from database, you'll keep this undefined
//if (response.data.actionType1.actionEffectiveDate !== null) {
// // only make an actual date if there is something stored
// var actionEffectiveDate = new Date(response.data.actionType1.actionEffectiveDate);
//}
//$scope.sanitizedActionEffectiveDate = $filter('date')(actionEffectiveDate, "yyyy-MM-dd")
$scope.sanitizedActionEffectiveDate = $scope.sanitizeDate(response.data.actionType1.actionEffectiveDate);
这是我尝试编写范围行为。
$scope.sanitizeDate = function(dirtyDate){
var tempDate = undefined; // if dirtyDate is null from database, you'll keep this undefined
if (dirtyDate !== null) {
// only make an actual date if there is something stored
var tempDate = new Date(dirtyDate);
}
tempDate = $filter('date')(tempDate, "yyyy-MM-dd");
return tempDate;
}
如果我在Web Developer工具中设置了一个断点,我可以看到dirtyDate正确进入,并且在我点击return语句之前tempDate看起来是正确的。
然后Angular抛出一个" rejectPromise"错误。 有没有正确的方法来做到这一点?
减少这个代码库会很不错。
答案 0 :(得分:0)
我忘了注入$ filter服务。 它现在正在运作。