0

I have controller (say BaseCtrl) that has a number of functions attached to it. I want to extend BaseCtrl to other controllers that shares some of its functions, however, I only need a function or two (and not all of BaseCtrl's functions). I already saw some posts that demonstrates how to extend a controller, but I wonder if it's possible to extend specific functions only and how to do it?

oikonomiyaki
  • 7,691
  • 15
  • 62
  • 101
  • You mean that you want to share the functions from BaseCtrl to other controllers, right? If this is your goal, use service or factory... – arman1991 Dec 15 '14 at 13:25
  • @arman1991 Although I already used service or factory before, I prefer not to use service or factory this time. I want to extend a controller's function, or if being specific is not possible, I'll just extend the whole controller. – oikonomiyaki Dec 15 '14 at 13:35
  • Maybe this articles can help you: http://stackoverflow.com/questions/16539999/angular-extending-controller; http://stackoverflow.com/questions/21483555/angular-js-best-practice-extending-controllers-overriding-controller-defaults – arman1991 Dec 15 '14 at 13:42
  • @arman1991 The reason I can't just use services/factories is that we're a team in a project, and I my team mates already coded controllers that I can re-use. – oikonomiyaki Dec 16 '14 at 03:41
  • @Ved Could you give an example on how to use $rootScope? – oikonomiyaki Dec 16 '14 at 03:41
  • Sure.. I will try to write a general example. – Ved Dec 16 '14 at 05:07

1 Answers1

2

Extending my comment:

 app.controller('parentCtrl', function($scope,$rootscope) {

$rootscope.myPerent = function () {
     //your code
         }

   });

app.controller('childCtrl', function($scope,$rootscope) {

    $scope.ourPerent = function () {
        $rootscope.myPerent();
   }

   });
Ved
  • 11,837
  • 5
  • 42
  • 60