Angular Model
AngularJS ng-model Directive
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
The ng-model Directive
With theng-model
directive you lot tin flaming bind the value of an input plain to a variable created inwards AngularJS.Example
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});</script>
Two-Way Binding
The binding goes both ways. If the user changes the value within the input field, the AngularJS holding volition besides alter its value:Example
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
Validate User Input
Theng-model
directive tin flaming render type validation for application information (number, e-mail, 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 valid e-mail address</span>
</form>
ng-show
attribute returns true
. If the holding inwards the
ng-model
attribute does non exist, AngularJS volition practice 1 for you.Application Status
Theng-model
directive tin flaming render condition for application information (invalid, dirty, touched, error):Example
<form ng-app="" name="myForm" ng-init="myText = 'post@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>
CSS Classes
Theng-model
directive provides CSS classes for HTML elements, depending on their status:Example
<style>
input.ng-invalid {
background-color: lightblue;
}
</style>
<body>
<form ng-app="" name="myForm">
Enter your name:
<input name="myName" ng-model="myText" required>
</form>
ng-model
directive adds/removes the next classes, according to the condition of the cast field:- ng-empty
- ng-not-empty
- ng-touched
- ng-untouched
- ng-valid
- ng-invalid
- ng-dirty
- ng-pending
- ng-pristine
Comments
Post a Comment