In angularJs I have a bunch of controllers with some funktions they are same:
angular.module('myApp').controller(...){
function lockForm(id){
...
}
function releaseForm(id){
...
}
function dbError(e){
...
}
// and some other little stuff again and again in every controller
}
The examples I've read are all based on extending (mixin) stuff from $scope and @this. Is'nt there a way to extend the whole controller as is?
With thanks to Shushanth Pallegar I solved my Problem. It's not exactly what I wanted but way better than before.
// base ctrl
angular.module('myApp').controller('baseViewCtrl', function ($scope, viewData) {
this.lockForm = function() {
viewData.isLoading = true;
};
// ... and others
});
// child ctrl
angular.module('myApp').controller('childCtrl', function ($scope) {
var viewData = {
// some stuff
};
// inject from basecontroller
angular.extend(this, $controller('baseViewCtrl', {$scope: $scope, viewData: viewData}));
// link $scope
$scope.viewData = viewData;
$scope.onSelectUnit = onSelectUnit;
// child controller methods
function onSelectUnit(){
this.lockForm();
...
}
});
It looks a bit ugly at all because I avoided @this nearly everywhere
Perhaps I go this way and use base instead of @this to make it more clear that there are injected methods:
// child ctrl
angular.module('myApp').controller('childCtrl', function ($scope) {
var viewData = {
// some stuff
};
// inject from basecontroller
var base = $controller('baseViewCtrl', {$scope: $scope, viewData: viewData});
// link $scope
$scope.viewData = viewData;
$scope.onSelectUnit = onSelectUnit;
// child controller methods
function onSelectUnit(){
base.lockForm();
...
}