After realizing that my controller has become too crowded with logic, I've decided to transfer some of it to a service for better organization and maintenance.
Currently, the controller handles a URL input from either YouTube or Vimeo, detecting the presence of "youtube" or "vimeo" in the string and executing the necessary actions accordingly. Here's a snippet of the existing "logic" within the controller:
if url.indexOf("youtube") > -1 {
variable_1 = "Something";
variable_2 = "Something";
//carry out additional tasks
}
else {
variable_1 = "Something";
variable_2 = "Something";
//do some more stuff
}
$scope.task.items.push("I need to add things to this array too");
Transitioning to a Service is the recommended approach, but the initial question arises: should I use a service
or a factory
?
This scenario prompts me to consider how to pass the variables (variable_1 and variable_2) back to the controller once the service completes its task.
myApp.service('urlService', function() {
this.detectProvider = function() {
if url.indexOf("youtube") > -1 {
}
else {
}
//how can I push things to the $scope array here?
};
});