WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AngularJS ng-model Directive


The ng-show order ties the estimation of HTML controls (input, select, textarea) to application data.


The ng-show Directive

With the ng-model mandate you can tie the estimation of an information field to a variable made in AngularJS.

Example

<div ng-app="myApp" ng-controller="myCtrl">
    Name: <input ng-model="name">
</div>

<script>
var application = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
});
</script>
Try it Yourself »

Two-Way Binding

The restricting goes both ways. In the event that the client changes the incentive inside the information field, the AngularJS property will likewise change it's value:

Example

<div ng-app="myApp" ng-controller="myCtrl">
    Name: <input ng-model="name">
    <h1>You entered: {{name}}</h1>
</div>
Try it Yourself »

Validate User Input

The ng-model order can give type approval to application information (number, email, required):

Example

<form ng-app="" name="myForm">
    Email:
    <input type="email" name="myAddress" ng-model="text">
    <span ng-show="myForm.myAddress.$error.email">Not a legitimate email address</span>
</form>
Try it Yourself »

In the precedent over, the range will be shown just if the articulation in the ng-show property returns true.


Application Status

The ng-model order can give status to application information (invalid, grimy, contacted, error):

Example

<form ng-app="" name="myForm" ng-init="myText = 'post@myweb.com'">
    Email:
    <input type="email" name="myAddress" ng-model="myText" required></p>
    <h1>Status</h1>
    {{myForm.myAddress.$valid}}
    {{myForm.myAddress.$dirty}}
    {{myForm.myAddress.$touched}}
</form>
Try it Yourself »

CSS Classes

The ng-model order gives CSS classes to HTML components, depending on their status:

Example

<style>
input.ng-invalid {
    foundation shading: lightblue;
}
</style>
<body>

<form ng-app="" name="myForm">
    Enter your name:
    <input name="myAddress" ng-model="text" required>
</form>
Try it Yourself »

The ng-model order includes/evacuates the accompanying classes, as per the status of the structure field:

  • ng-empty
  • ng-not-empty
  • ng-touched
  • ng-untouched
  • ng-valid
  • ng-invalid
  • ng-dirty
  • ng-pending
  • ng-pristine