Skip to content

Instantly share code, notes, and snippets.

@m00s
Last active January 20, 2016 08:45
Show Gist options
  • Save m00s/a7acd8ea9bffe5cdc678 to your computer and use it in GitHub Desktop.
Save m00s/a7acd8ea9bffe5cdc678 to your computer and use it in GitHub Desktop.
Angular Controller Inheritance (Live fiddle https://jsfiddle.net/m00s/tkoprctu/5/)
angular
.module('myApp', [])
.controller('baseController', baseCtrl)
.controller('childController', childCtrl);
function baseCtrl() {
var vm = this;
vm.message = 'Base controller!';
// Shared logic here
}
childCtrl.$inject = ['$controller'];
function childCtrl($controller) {
var vm = this;
vm = angular.extend(this, $controller('baseController'));
// Ovverride base scope
vm.message = 'Child controller!';
}
<body ng-app="myApp">
<div ng-controller='baseController as vm' ng-cloak>
<h1> {{ vm.message }} </h1>
</div>
<div ng-controller='childController as vm' ng-cloak>
<h1> {{ vm.message }} </h1>
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment