Last active
January 20, 2016 08:45
-
-
Save m00s/a7acd8ea9bffe5cdc678 to your computer and use it in GitHub Desktop.
Angular Controller Inheritance (Live fiddle https://jsfiddle.net/m00s/tkoprctu/5/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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