I'm using Angular 1.4.8 and am trying to extend a controller. Both the original controller and the extending controller are extremely similar, but there's a function I want to override in the extending controller.
angular.module('app').controller('ControllerOne', function() {
var vm = this;
vm.data = {};
vm.callSayHello = function() {
vm.sayHello();
}
vm.sayHello = function() {
console.log('Hello from ControllerOne.');
}
});
angular.module('app').controller('ControllerTwo', ['$scope', function($scope) {
angular.extend(vm, $controller('OrdersAddController', { $scope: $scope }));
// I do not want to override vm.callSayHello(), so I don't redeclare that function.
// This is what I want to override.
vm.sayHello = function() {
console.log('Hello from ControllerTwo.');
// Some other extra handling.
}
}]);
<button type="button" ng-click="ctrl.callSayHello()">Click Me</button>
It looks like I can override the ControllerOne functions in ControllerTwo. But for this particular case, the function I want to override, vm.sayHello(), isn't directly called from an event like a click, but rather it's called by another function, vm.callSayHello().
So what happens is that when vm.callSayHello() is invoked from either ControllerOne or ControllerTwo, that also calls vm.sayHello(), but it always calls the function on ControllerOne despite being redeclared in ControllerTwo.
Hopefully that makes sense. But is there a way to just override that vm.sayHello() function in ControllerTwo?