Call function in scope to do calculation in expression

Description

The following code shows how to Call function in scope to do calculation in expression.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
var  MedicationsCtrl = function($scope) {
  $scope.medications = [<!-- w  w w  .ja v  a  2 s. co m-->
    { dosage : 0.5, count : 10},
    { dosage : 0.2, count : 5}
  ];
};
var MedicationCtrl = function($scope) {
    $scope.total = function(medication) {
      return medication.dosage * medication.count;
    };
};
</script>
</head>
<body>
  <div ng-app ng-controller="MedicationsCtrl">
  <ul ng-repeat="m in medications" ng-controller="MedicationCtrl">
    <li>{{m.dosage}} + {{m.count}} = {{total(m)}}</li>
  </ul>
</div>
</body>
</html>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities