Encapsulating a Model Value with a Controller Function


You wish to retrieve a model via function (instead of directly accessing the scope from the template) that encapsulates the model value.


Define a getter function that returns the model value.

function MyCtrl($scope) {
  $scope.value = 1;

  $scope.getIncrementedValue = function() {
    return $scope.value + 1;

Then in the template we use an expression to call it:

<div ng-controller="MyCtrl">


MyCtrl defines the getIncrementedValue function, which uses the current value and returns it incremented by one. One could argue that depending on the use case it would make more sense to use a filter. But there are use cases specific to the controllers behavior where a generic filter is not required.