The binding goes both ways. If the user changes the value inside the input field, the AngularJS property will also change its value.
<body> <div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="name"> <h1>You entered: {{name}}</h1> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.name = "Default Name"; }); </script> </body>
The ng-model directive adds/removes the following classes, according to the status of the form field:
<style> input.ng-invalid { background-color: #fcc; } input.ng-valid { background-color: #ffa; } </style> <body> <form ng-app="" name="myForm"> Enter your name: <input ng-model="firstName" placeholder="firstname" required> <input ng-model="lastName" placeholder="lastname" required>* </form> <p>Textbox backcolor will automatically changed according to valid or invalid status.</p> </body>
<style> input.ng-empty { background-color: #fcc; } input.ng-not-empty { background-color: #ffa; } </style>
<style> input.ng-touched { background-color: #fcc; } input.ng-untouched { background-color: #ffa; } </style>
The ng-model directive can provide validation for application data like e-mail, number, required.
<form ng-app="" name="myForm"> Email: <input type="email" name="emailid" ng-model="text"> <span ng-show="myForm.emailid.$error.email">Not a valid e-mail address</span> </form>
<form ng-app="" name="myForm" ng-init="myText = 'email@myweb.com'"> Email: <input type="email" name="myAddress" ng-model="myText" required> <h1>Status</h1> {{myForm.myAddress.$valid}} {{myForm.myAddress.$dirty}} {{myForm.myAddress.$touched}} </form>