Search code examples
angularjsangularjs-serviceangularjs-controller

Logic in the controller that isn't assigned directly to the scope should be fulfilled else where before it gets to the controller


 $scope.listValueChanged = function (name) {
 listId = [];
 $scope.listType= name;
 };

 if ($scope.contacts &&   $scope.contacts.length > 0) {
       var gsSelectedContacts =$scope.contacts;
      for (var i = 0; i < $scope.contacts.length; i++) {
         contactIds.push(gsSelectedContacts[i].id);
       }
 }

for the above code I got review comments from my superior like below Logic in the controller that isn't assigned directly to the scope should be fulfilled elsewhere before it gets to the controller, then applied to the $scope. I am using this method at one place only. Do, i really need to move this logic to services?


Solution

  • Well it depends on whats the use case here . If you are going to reuse something its better to create a service for that but if you are not going to reuse or the code is not enough to be used as a service then you should avoid using service . Service are meant to make your code more organized and give better usability so it depends on whats you're requirement really is.

    details on where to use service can be found here