Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何从角度行为返回值并避免错误?_Angularjs - Fatal编程技术网

Angularjs 如何从角度行为返回值并避免错误?

Angularjs 如何从角度行为返回值并避免错误?,angularjs,Angularjs,我在Angular中有一个get操作,它调用我的WebApi get服务。然后,我使用结果填充详细信息表单或编辑表单。如果我正在填充一个编辑表单,那么我需要清理日期,以便HTML5日期选择器从响应数据中显示正确的日期 以下是我的角度获取方法: $scope.getAction = function (actionId, populateObject) { $http.get(actionUrl + '/' + actionId) .then(func

我在Angular中有一个get操作,它调用我的WebApi get服务。然后,我使用结果填充详细信息表单或编辑表单。如果我正在填充一个编辑表单,那么我需要清理日期,以便HTML5日期选择器从响应数据中显示正确的日期

以下是我的角度获取方法:

    $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;
    }
如果我在WebDeveloper工具中设置一个断点,我可以看到dirtyDate正确输入,而tempDate在返回语句之前看起来是正确的

但是Angular抛出了一个“拒绝承诺”错误。 有没有合适的方法


减少这个代码库会很好。

我只是忘了注入$filter服务。 它正在工作