AngularJS Tutorial - ng-model








The ng-model directive binds an input, select, textarea, or other form controls to a property on the scope.

If the property doesn't already exist, it will be created implicitly and added to the scope.

<input type="text"
      ng-model="myModel.myProperty"  />

Example

The following code shows how to ng-model.


<!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  .ja  v a2 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: