Angular: Extending controller

What's the recommended way in Angular for extending controllers? I have three controllers that are quite similar. I want to have a controller which these three extend and share its functions.


Perhaps you don't extend a controller but it is possible to extend a controller or make a single controller a mixin of multiple controllers.

module.controller('CtrlImplAdvanced', ['$scope', '$controller', function ($scope, $controller) {
    // Initialize the super class and extend it.
    angular.extend(this, $controller('CtrlImpl', {$scope: $scope}));
    … Additional extensions to create a mixin.

When the parent controller is created the logic contained within it is also executed. See $controller() for for more information about but only the $scope value needs to be passed. All other values will be injected normally.

@mwarren, your concern is taken care of auto-magically by Angular dependency injection. All you need is to inject $scope, although you could override the other injected values if desired. Take the following example:

(function(angular) {

	var module = angular.module('stackoverflow.example',[]);

	module.controller('simpleController', function($scope, $document) {
		this.getOrigin = function() {
			return $document[0].location.origin;

	module.controller('complexController', function($scope, $controller) {
		angular.extend(this, $controller('simpleController', {$scope: $scope}));

<script src=""></script>

<div ng-app="stackoverflow.example">
    <div ng-controller="complexController as C">
        <span><b>Origin from Controller:</b> {{C.getOrigin()}}</span>

For inheritance you can use standard JavaScript inheritance patterns. Here is a demo which uses $injector

function Parent($scope) {
  $ = 'Human';
  $scope.clickParent = function() {
    $ = 'Clicked from base controller';

function Child($scope, $injector) {
  $injector.invoke(Parent, this, {$scope: $scope});
  $ = 'Human Child';
  $scope.clickChild = function(){

Child.prototype = Object.create(Parent.prototype);

In case you use the controllerAs syntax (which I highly recommend), it is even easier to use the classical inheritance pattern:

function BaseCtrl() { = 'foobar';
BaseCtrl.prototype.parentMethod = function () {

function ChildCtrl() {; = 'baz';
ChildCtrl.prototype = Object.create(BaseCtrl.prototype);
ChildCtrl.prototype.childMethod = function () {

app.controller('BaseCtrl', BaseCtrl);
app.controller('ChildCtrl', ChildCtrl);

Another way could be to create just "abstract" constructor function which will be your base controller:

function BaseController() { = function () {
    //some actions here

module.controller('ChildCtrl', ['$scope', function ($scope) {$scope);
  $scope.anotherClick = function () {
    //other actions

Blog post on this topic

You don't extend controllers. If they perform the same basic functions then those functions need to be moved to a service. That service can be injected into your controllers.

Well, I'm not exactly sure what you want to achieve, but usually Services are the way to go. You can also use the Scope inheritance characteristics of Angular to share code between controllers:

<body ng-controller="ParentCtrl">
 <div ng-controller="FirstChildCtrl"></div>
 <div ng-controller="SecondChildCtrl"></div>

function ParentCtrl($scope) {
 $scope.fx = function() {
   alert("Hello World");

function FirstChildCtrl($scope) {
  // $scope.fx() is available here

function SecondChildCtrl($scope) {
  // $scope.fx() is available here

You can create a service and inherit its behaviour in any controller just by injecting it.

app.service("reusableCode", function() {

    var reusableCode = {};

    reusableCode.commonMethod = function() {
        alert('Hello, World!');

    return reusableCode;

Then in your controller that you want to extend from the above reusableCode service:

app.controller('MainCtrl', function($scope, reusableCode) {

    angular.extend($scope, reusableCode);

    // now you can access all the properties of reusableCode in this $scope



You can try something like this (have not tested):

function baseController(callback){
    return function($scope){
        $scope.baseMethod = function(){
            console.log('base method');
        callback.apply(this, arguments);

app.controller('childController', baseController(function(){


Yet another good solution taken from this article:

// base controller containing common functions for add/edit controllers
module.controller('Diary.BaseAddEditController', function ($scope, SomeService) {
    $scope.diaryEntry = {};

    $scope.saveDiaryEntry = function () {

    // add any other shared functionality here.

module.controller('Diary.AddDiaryController', function ($scope, $controller) {
    // instantiate base controller
    $controller('Diary.BaseAddEditController', { $scope: $scope });

module.controller('Diary.EditDiaryController', function ($scope, $routeParams, DiaryService, $controller) {
    // instantiate base controller
    $controller('Diary.BaseAddEditController', { $scope: $scope });

    DiaryService.GetDiaryEntry($ (data) {
        $scope.diaryEntry = data;

You can directly use $controller('ParentController', {$scope:$scope}) Example module.controller('Parent', ['$scope', function ($scope) { //code }])

module.controller('CtrlImplAdvanced', ['$scope', '$controller', function ($scope, $controller) { //extend parent controller $controller('CtrlImpl', {$scope: $scope}); }]);

You can extend with a services, factories or providers. they are the same but with different degree of flexibility.

here an example using factory :


.factory('myFactory', function() {
    var myFactory = {
        save: function () {
            // saving ...
        store: function () {
            // storing ...
    return myFactory;

.controller('myController', function($scope, myFactory) {
    $scope.myFactory = myFactory;; // here you can use the save function

And here you can use the store function also:

<div ng-controller="myController">
    <input ng-blur="" />

You can use Angular "as" syntax combined with plain JavaScript inheritance

See more details here

I wrote a function to do this:

function extendController(baseController, extension) {
    return [
        '$scope', '$injector',
        function($scope, $injector) {
            $injector.invoke(baseController, this, { $scope: $scope });
            $injector.invoke(extension, this, { $scope: $scope });

You can use it like this:

function() {
    var BaseController = [
        '$scope', '$http', // etc.
        function($scope, $http, // etc.
            $scope.myFunction = function() {

            // etc.

            ['$scope', '$filter', // etc.
            function($scope, $filter /* etc. */)
                $scope.myOtherFunction = function() {

                // etc.


  1. You don't have to register the base controller.
  2. None of the controllers need to know about the $controller or $injector services.
  3. It works well with angular's array injection syntax - which is essential if your javascript is going to be minified.
  4. You can easily add extra injectable services to the base controller, without also having to remember to add them to, and pass them through from, all of your child controllers.


  1. The base controller has to be defined as a variable, which risks polluting the global scope. I've avoided this in my usage example by wrapping everything in an anonymous self-executing function, but this does mean that all of the child controllers have to be declared in the same file.
  2. This pattern works well for controllers which are instantiated directly from your html, but isn't so good for controllers that you create from your code via the $controller() service, because it's dependence on the injector prevents you from directly injecting extra, non-service parameters from your calling code.

I consider extending controllers as bad-practice. Rather put your shared logic into a service. Extended objects in javascript tend to get rather complex. If you want to use inheritance, I would recommend typescript. Still, thin controllers are better way to go in my point of view.

Need Your Help

i want to crop a .tif format image using php

php crop

i want to crop a .tif format image using php .

Issue with Paint Event on Mac OSX 10.8.5 in wxPython

python osx wxpython paint paintevent

This has me at my wit's end right now. I'm relatively new to wxPython, and I'm editing someone else's code so please bare with me.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.