I currently have multiple controllers responsible for fetching data from the server. One example of such a controller is shown below:
var vm = this;
var month = 1;
loadStatusCount();
function loadStatusCount() {
vm.summaryCount = [];
statusCountService.setMonth(month);
statusCountService.setType('e');
statusCountService.getStatusCount()
.then(function (report) {
applyCount(report);
});
}
function applyCount(report) {
vm.summaryCount = report[0];
}
vm.getTotal = function () {
var total = 0;
for (var i = 0; i < vm.summaryCount.length; i++) {
var count = vm.summaryCount[i].count;
total = total + count;
}
return total;
}
The rest of the controllers have similar code, the only difference being the type
parameter.
As of now, I have a directive that displays a template.
monthlyReportApp.directive('statusCount', function () {
return {
restrict: 'AE',
replace: false,
templateUrl: 'Scripts/app/views/templates/status-count-template.html',
link: linker
}
function linker(scope, el, attrs, ngModel) {
scope.title = attrs.title;
scope.class = attrs.class;
}
});
I utilize this directive in my HTML like so:
<div status-count data-class="fa fa-check-square-o" data-title="Prior Auth Count" class="panel panel-default" ng-controller="PACountCtrl as ctrl">
</div>
While it's not a pressing issue, I am interested in reducing redundancy. Hence, instead of creating multiple similar controllers, how can I have a single controller that can be utilized by the directive to fetch data with different type
values for various div
elements?