AngularJS Tutorial - Call function in scope to do calculation in expression








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 = [<!--from   www  .  j a  v  a 2 s .  c  om-->
    { 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>

The code above is rendered as follows: