I have a dilemma with two controllers that are responsible for similar tasks.
How can I efficiently eliminate code duplication?
Initially, my attempt was to create a template method as an angular service. However, I noticed that it's not possible to inject $scope into services.
.controller("TspController", function($scope, $rootScope, $routeParams, $location, ProblemsLoader) {
$scope.problemType = "tsp";
var problemId = $routeParams.problemId;
ProblemsLoader.loadIndex("tsp", function(index) {
if (problemId) {
$scope.problems = index;
ProblemsLoader.loadProblem("tsp", problemId, function(problem) {
$scope.problem = problem
});
} else {
var firstProblemId = index[0].fileName;
$location.path("/tsp/" + firstProblemId)
}
});
})
.controller("VrpController", function($scope, $rootScope, $http, $routeParams, ProblemsLoader) {
$scope.problemType = "vrp";
var problemId = $routeParams.problemId;
ProblemsLoader.loadIndex("vrp", function(index) {
if (problemId) {
$scope.problems = index;
ProblemsLoader.loadProblem("vrp", problemId, function(problem) {
$scope.problem = problem
});
} else {
var firstProblemId = index[0].fileName;
$location.path("/vrp/" + firstProblemId)
}
});
});