3
Below is my code that implements inheritance using $controller. Is this way good? My goal is to reuse a baseController class that will have some common routines for crud controllers.
Fiddle: http://jsfiddle.net/1o278ntw/6/
var myApp = angular.module('myApp', []);
myApp.controller('parentController', parentController);
function parentController() {
var vm = this;
vm.mainRoutine = mainRoutine;
vm.subRoutine = subRoutine;
//1: use the vm before call subRoutine, just solved the future override
function mainRoutine() { return vm.subRoutine(); };
function subRoutine() { return 'parent'; };
}
myApp.controller('child1Controller', child1Controller);
child1Controller.$inject = ['$controller'];
function child1Controller($controller) {
var vm = this;
var base = $controller('parentController');
angular.extend(vm, base);
//2: override
base.subRoutine = subRoutine;
vm.childRoutine = childRoutine;
vm.callParentRoutine = callParentRoutine;
function subRoutine() { return vm.subRoutine() + ' -> child1'; };
function childRoutine() { return 'childRoutine'; };
function callParentRoutine() { return vm.mainRoutine(); };
}
Hi @Rodrigo, I didn’t get to do the test with ui-router, but I think it will give a result similar to that below, don’t you think? dai would not work for what I wanted, which in case is to give an override in a routine (subRoutine) where another routine (mainRoutine) of the parent class sees. Ex. of how ui-router should behave: http://jsfiddle.net/3gr9we9m/
– Ricardo Carvalho