Encapsulate a Model Value with a Controller

Description

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 va 2  s.com-->
<!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.

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities