AngularJS Tutorial

Define AngularJS

Download AngularJS

AngularJS Expression

AngularJS vs Javascript

AngularJS Directives

AngularJS Module

AngularJS Controller

Custom Directive

AngularJS Scope

AngularJS Validation

AngularJS Filter

AngularJS SelectBox

AngularJS Directive

AngularJS Services

AngularJS Directives

AngularJS Routing

Interview Questions

Two-Way Binding

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>

AngularJS Validation Directives

The ng-model directive adds/removes the following classes, according to the status of the form field:

ng-valid / ng-invalid Directive

<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>  

ng-empty / ng-not-empty Directive

<style>
input.ng-empty {
	background-color: #fcc;
}

input.ng-not-empty {
	background-color: #ffa;
}
</style>

ng-touched / ng-untouched Directive

<style>
input.ng-touched {
	background-color: #fcc;
}

input.ng-untouched {
	background-color: #ffa;
}
</style>

Validate User Input

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>