Menu ▼

ng-model directive in AngularJS

The ng-model directive is used to bind the value to HTML controls and it is responsible for

  • Control validations like required, number, url, email.
  • Registering the control with parent form
  • Maintaining the state of the control like valid, invalid, dirty, touched, untouched
  • Applying related classes on the element like ng-valid, ng-dirty, etc

As AngularJS provides two way binding, The scope property assign to the model will be applied to the control value, and any changes done on the control value will applied to scope property.

Enter Quantity :
Enter Unit Price :

Quantity : {{Qty}}
Unit Price : {{UnitPrice}}
Total Price : {{Qty*UnitPrice}}

Source code:
<html xmlns="">
    <script src=""></script>
    <div ng-app="SampleApp" ng-controller="SampleCntrl">
                    Enter Quantity :
                    <input type="text" ng-model="Qty" /><br />
                    Enter Unit Price :
                    <input type="text" ng-model="UnitPrice" /><br />
        <br />
        Quantity : {{Qty}}<br />
        Unit Price : {{UnitPrice}}<br />
        Total Price : {{Qty*UnitPrice}}<br />
        var myapp = angular.module("SampleApp", []);
        myapp.controller("SampleCntrl", function ($scope) {
            $scope.Qty = 5;
            $scope.UnitPrice = 6;

comments powered by Disqus