在角度js上重构代码重复(任何建议?)

时间:2014-04-25 08:43:47

标签: javascript angularjs angularjs-directive angular-ui

有关如何使我的代码更清晰且没有代码的任何建议 重复?正如你所看到的,有很多重复的声明。

$scope.getStatistics = function(startDate, endDate) {
    var start  = startDate;
    var date1  = start.getDate();
    var month1 = (start.getMonth() +1);
    var year1  = start.getFullYear();
    var end    = endDate;
    var date2  = end.getDate();
    var month2 = (end.getMonth() +1);
    var year2  = end.getFullYear();
    $http.get('/admin/api/stats?start=' + date1 + '-' + month1 + '-' + year1 + '&end=' + date2 + '-' + month2 + '-' + year2).success(function(data) {
        $scope.stats = data;
    });
}
$scope.getDiffPrev = function(startDate, endDate, computeDiff) {
        var start = angular.copy(startDate)
        start.setDate(start.getDate() - 7);
        var date1 = start.getDate();
        var month1 = start.getMonth() + 1; 
        var year1 = start.getFullYear();
        var end = angular.copy(endDate)
        end.setDate(end.getDate() - 7);
        var date2 = end.getDate();
        var month2 = end.getMonth() + 1; 
        var year2 = end.getFullYear();
    $http.get('/admin/api/stats?start=' + date1 +'-'+ month1 +'-'+ year1 + '&end=' + date2 +'-'+ month2 +'-'+ year2 +'&computeDiff=true').success(function(data) {
        $scope.stats = data;
    });
}

2 个答案:

答案 0 :(得分:0)

function formatDate(date1){
    var day1  = date1.getDate();
    var month1 = (date1.getMonth() +1);
    var year1  = date1.getFullYear();
    return day1 + '-' + month1 + '-' + year1
}

$scope.getStatistics = function(startDate, endDate) {
    $http.get('/admin/api/stats?start=' + formatDate(startDate) + '&end=' + formatDate(endDate)).success(function(data) {
        $scope.stats = data;
    });
}
$scope.getDiffPrev = function(startDate, endDate, computeDiff) {
        var start = angular.copy(startDate)
        start.setDate(start.getDate() - 7);
        var end = angular.copy(endDate)
        end.setDate(end.getDate() - 7);
    $http.get('/admin/api/stats?start=' + formatDate(start) + '&end=' + formatDate(end) +'&computeDiff=true').success(function(data) {
        $scope.stats = data;
    });
}

答案 1 :(得分:0)

您可以创建单件服务以避免代码重复,使用此对象可以访问应用程序中的其他位置。 例如,您可以使用作为对象,我不知道确切的代码,但它会帮助您。

angular.module('dateService', [ngResource])
.factory('DateService',['$http', function($http) {
    var start, date1, month1, year1, end, date2, month2, result;
    return {
        date = function(startDate, endDate) {
            start  = startDate;
            date1  = start.getDate();
            month1 = (start.getMonth() +1);
            year1  = start.getFullYear();
            end    = endDate;
            date2  = end.getDate();
            month2 = (end.getMonth() +1);
            year2  = end.getFullYear();

            $http.get('/admin/api/stats?start=' + date1 + '-' + month1 + '-' + year1 + '&end=' + date2 + '-' + month2 + '-' + year2).success(function(data) {
                result = data;
            });
            return result;
        }
    }
}]);



 angular.module('module-name', [dateService])
.cotroller('controller_name', ['DateService', function(dateService) {

    $scope.getStatistics = function(startDate, endDate) {

        var result = DateService.date(startDate, endDate);
        result.then(function(data) {
            $scope.start = data;
        });

    };

    $scope.getDiffPrev = function(startDate, endDate, computeDiff) {
        var result = DateService.date(startDate, endDate);
        result.then(function(data) {
            $scope.start = data;
        });
    };
}]);