2

I have two controllers. There are functions that are identical and others that are differ.

For example:

Controller 1:

app.controller('ControllerOne', ['$scope','HelperService',
    function ($scope, HelperService) {
        $scope.helperService = HelperService; //get an instance of the HelperService

        $scope.select = function () {
            $scope.helperService.doA();
            $scope.helperService.doB();
            $scope.helperService.doC();
        };

        $scope.filter = function () {
            $scope.helperService.doB();
            $scope.helperService.doC();
            $scope.helperService.doE();
            $scope.helperService.doF();
        };
    }
]);

The second controller:

app.controller('ControllerOne', ['$scope','HelperService',
    function ($scope, HelperService) {
        $scope.helperService = HelperService; //get an instance of the HelperService

        $scope.select = function () {
            $scope.helperService.doA();
            $scope.helperService.doB();
            $scope.helperService.doC();
            $scope.helperService.doD();
        };

        $scope.filter = function () {
            $scope.helperService.doB();
            $scope.helperService.doC();
            $scope.helperService.doD();
            $scope.helperService.doF();
        };
    }
]);

I have partials html to use the same click event but it depends on the controller:

<div ng-click="select()"></div> 

How can I avoid from duplicated code on both controllers?

JoriO
  • 1,050
  • 6
  • 13
Aviade
  • 2,057
  • 4
  • 27
  • 49
  • 2
    See [enter link description here][1] for how to extend controllers. [1]: http://stackoverflow.com/questions/16539999/angular-extending-controller – Enzey Nov 25 '14 at 07:52

0 Answers0