AngularJS Tutorial - Bind Form to scope








The following code shows how to Bind Form to scope.

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 MedicationCtrl = function($scope) {
    $scope.medication = {<!--   w ww  .  j  a v  a  2  s  . co m-->
        dosage : 0.5,
        count : 10,
        total : function() {
            return this.dosage * this.count;
        }
    };
}
</script>
</head>
<body>
  <div ng-app ng-controller="MedicationCtrl">
dosage : <input type="number" ng-model="medication.dosage"><br>
count : <input type="number" ng-model="medication.count">
<hr>
Total: {{medication.total()}}
</div>
</body>
</html>

The code above is rendered as follows: