Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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
Javascript 重构angular js上的代码复制(有什么建议吗?)_Javascript_Angularjs_Angularjs Directive_Angular Ui - Fatal编程技术网

Javascript 重构angular js上的代码复制(有什么建议吗?)

Javascript 重构angular js上的代码复制(有什么建议吗?),javascript,angularjs,angularjs-directive,angular-ui,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

关于如何使我的代码更干净,没有代码,有什么建议吗 重复?正如您所看到的,有很多重复的声明

$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;
    });
}

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

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;
        });
    };
}]);
这不应该贴在网上吗?
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;
        });
    };
}]);