Angularjs Example PDF
Angularjs Example PDF
Created By:
1
Copyright © pTutorial · All Rights Reserved
Angular Directive Example
This AngularJS Directive example explain the concept behind the ng-app,
ng-model, ng-init, ng-model, ng-repeat.
Directives Example
<html>
<head>
<title>AngularJS Directives Example</title>
<script src = "angular.min.js"></script>
</head>
<body>
<h2>Simple Directives Application</h2>
<p>List of Cities</p>
<ol>
<li ng-repeat = "x in cities">
{{ x }}
</li>
</ol>
</div>
</body>
</html>
Output
2
Copyright © pTutorial · All Rights Reserved
Simple Directives Application
Type Name:
Hello
List of Cities
1. Delhi
2. Noida
3. Gurgaon
4. Faridabad
Explanation
Cities is a simple array variable initialize usig ng-init directive. The array variable
having four cities.
Ng-bind is used to display the data of ng-model. You can use Angular
Expression in place of ng-bind.
3
Copyright © pTutorial · All Rights Reserved
Simple AngularJS Example to get the data form the server $http can be
used to get the data from server.
<!DOCTYPE html>
<html>
<script src= "angular.min.js"></script>
<body>
<table border="2">
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<tr ng-repeat="x in data">
<td>{{ x.Name}}</td>
<td>{{ x.City}}</td>
<td>{{ x.Country}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('ukApp', []);
app.controller('ukController', function($scope, $http) {
$http.get("data1.php")
.success(function(response) {$scope.data = response.studentdata;});
});
</script>
</body>
</html>
4
Copyright © pTutorial · All Rights Reserved
Explanation
If success, the controller assign the response data to the data property.
{
"studentdata": [
{
"Name" : "Umar Farooque",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "Umar",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "Rahul",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "John",
"City" : "New York city",
5
Copyright © pTutorial · All Rights Reserved
"Country" : "USA"
}
]
}
<!DOCTYPE html>
<html>
<script src= "angular.min.js"></script>
<body>
<table border="2">
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<tr ng-repeat="x in data">
<td>{{ x.Name}}</td>
<td>{{ x.City}}</td>
<td>{{ x.Country}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('ukApp', []);
app.controller('ukController', function($scope, $http) {
$http.get("data2.php")
.success(function(response) {$scope.data = response;});
});
</script>
</body>
</html>
6
Copyright © pTutorial · All Rights Reserved
Http://Www.Ptutorial.Com/Angularjs/Data2.Php
[
{
"Name" : "Umar Farooque",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "Umar",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "Rahul",
"City" : "Delhi",
"Country" : "India"
},
{
"Name" : "John",
"City" : "New York city",
"Country" : "USA"
}
You can use filter to a directive with pipe character and filter followed by the
colon ng-model name or string. Filter select from an array or JSON data.
Syntax
.....
<tr ng-repeat="x in data | filter:name">
.....
</tr>
7
Copyright © pTutorial · All Rights Reserved
Filter Example
<!DOCTYPE html>
<html>
<script src= "angular.min.js"></script>
<!DOCTYPE html>
<html>
<script src= "angular.min.js"></script>
<body>
<script>
var app = angular.module('ukApp', []);
app.controller('ukController', function($scope, $http) {
$http.get("data2.php")
.success(function(response) {$scope.data = response;});
});
</script>
</body>
</html>
8
Copyright © pTutorial · All Rights Reserved
Explanation
In the above example filter is associate with the ng-model = "name that why list
filter according to the input field with name.
If $http.get() method read the data successfully from the server the controller
create the data property.
If you ever think an AngularJS Example is not explained clearly or think we should
add a specific AngularJS Example suggest me at [email protected]. We will add
AngularJS Example as soon as possible for better experience.
9
Copyright © pTutorial · All Rights Reserved
10
Copyright © pTutorial · All Rights Reserved