I am working on a small piece of code which is being handled inside of a directive. The directive is passed a large object of data that's separated into types and subtypes. Using drop downs the user is meant to be able to select which part of the data they want to see in the display, and it should be dynamic, meaning that as they change their selection, the data would immediately update. My first attempt to do this was to put code into a controller attached to the directive.
Using a controller attached to the directive
View
<select ng-model="type" ng-change="updateDisplay()">
<option value="typeOne">One</option>
<option value="typeTwo">Two</option>
<option value="typeThree">Three</option>
</select>
<select ng-model="subType" ng-change="updateDisplay()">
<option value="subTypeOne">One</option>
<option value="subTypeTwo">Two</option>
<option value="subTypeThree">Three</option>
</select>
...
<div ng-repeat="(key, value) in selectedData">
{{key}}, {{value}}
</div>
Controller
$scope.type = 'typeOne';
$scope.subType = 'subTypeOne';
$scope.selectedData = null;
$scope.updateDisplay = function() {
//$scope.data is a large object of data passed into the directive, but not important to define for this post
$scope.selectedData = $scope.data[$scope.type][$scope.subType];
}
$scope.updateDisplay(); // call on page load to populate with defaults
This approach works fine, but it has been suggested to me that this is not the proper "angular" way and that all of this should be deferred to a service. In this case, not only would the data need to be passed to my directive, but all of the service methods as well. On the select drop downs, we would need to put a ng-change call to let the service know that the value has changed and needs to be updated
ng-change="setSelectedType({type:type})"
and then another function presumably would need to run after that "Setter" function has run, in order to recalculate the dynamic value for selectedData. (an equivalant of running updateDisplay() above.
My overall question is if moving all this logic to a service even makes sense? I would have to write getters and setters on the service side and call them each time on ng-change in addition to having the service then return the filtered data. Conversely if it is done in a controller we don't need to create any "setters/getters" for the variables, because they are bound to the scope and update automatically. (and to me the whole point of using a framework like angular).
Without making this question TOO opinionated, can someone with Angular experience talk about this?