Because functions are first-class citizens in JavaScript, we can pass them around like we would with any other variable.
See this JSFiddle example that alternates f1 and f2 when you click on Change Handler.
HTML
<body ng-app="exampleApp" ng-controller="appCtrl">
<div>
<h3>{{name}}</h3>
<br>
<button type="button" class="btn btn-primary" ng-click="fun()">Show Name</button>
<button type="button" class="btn btn-primary" ng-click="changeClick()">
Change Handler
</button>
<br>
<h3 ng-show="clicked">You did it {{name}}</h3>
</div>
</body>
JavaScript
var app = angular.module('exampleApp', []);
app.controller('appCtrl', function($scope){
function f1() {
$scope.name = "Chris";
}
function f2() {
$scope.name = "Harry";
}
$scope.fun = f1;
$scope.changeClick = function() {
if($scope.fun === f1) {
$scope.fun = f2;
} else {
$scope.fun = f1;
}
};
$scope.name = "Default";
});
In the example above, changeClick alternates the value of fun