AngularJS Tutorial - Encapsulate a Model Value with a Controller








To retrieve a model via function instead of directly accessing the scope from the template.

Example

The following code shows how to encapsulate a Model Value with a Controller.


<!--from  w  w  w  .  j  a v a2  s .  c o  m-->
<!doctype html>
<html ng-app>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
    <script>
function MyCtrl($scope) {
  $scope.value = 1;

  $scope.getIncrementedValue = function() {
    return $scope.value + 1;
  };
}    
    </script>
    <link rel="stylesheet" href="css/bootstrap.css">
  </head>
  <body ng-app>
    <div ng-controller="MyCtrl">
      <p>{{getIncrementedValue()}}</p>
    </div>
  </body>
</html>

The code above 
defined a getter function that returns the model value.

The code above is rendered as follows: