Answer the question
In order to leave comments, you need to log in
How to correctly inherit a controller in angular?
Essence of the question. There are two controllers:
angular.module('app', [])
.controller('AbstractCtrl', AbstractCtrl)
.controller('AppCtrl', AppCtrl);
function AbstractCtrl(){
var self = this;
self.name = "Vova";
self.init = function(){
self.name = "Petya";
};
}
function AppCtrl($controller){
var vm = this;
angular.extend(vm, $controller('AbstractCtrl', {}));
vm.init();
}
function AppCtrl(){
var vm = this;
AbstractCtrl.call(this);
vm.init();
}
Answer the question
In order to leave comments, you need to log in
function AppCtrl($scope, $controller) {
$controller('AbstractCtrl', { $scope: $scope });
}
// или
function AppCtrl($controller) {
$controller('AbstractCtrl', { vm: this });
}
I myself see as an option to write a factory that returns the desired function, which can be further injected into the controller and called via .call. But this is some kind of crutch and is not suitable for non-abstract controllers
Move the common functionality to a service, and in each controller, merge this service from this controller
If you need a common function for many controllers, create a factory
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question