Tuesday, April 21, 2015

angularjs use of ng-repeat loop

<html>
<title> AngularJs Directives </title>

<body>

<div ng-app="" ng-controller="firstController">

<p>List of Countries with locale:</p>
<ol>
{{ data }}
<li ng-repeat="item in data">

{{ 'firstname: ' + item.firstname + ', lastname: ' + item.lastname }}

</li>
</ol>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

<script>

function firstController($scope)
{

$scope.data=[
{
firstname:'shekhar',
lastname:'nawale'
},
{
firstname:'prashant',
lastname:'adpawar'
},
{
firstname:'abhishek',
lastname:'tripathi'
},
]



}

</script>

</body>
</html>

AngularJs ng-repeat directive

ng-repeat directive repeats HTML elements for each item in a collection. In the following example, we iterate over the array of countries.

<div ng-app="">
...
 <p>List of Countries with locale:</p>
 <ol>
 <li ng-repeat="country in countries">
 {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}
 </li>
 </ol>
</div> 

Angularjs ng-model directive

The ng-model directive defines the model/variable to be used in AngularJS Application. In the following example, we define a model named name.

<p> Enter Name : <input type="text" ng-model="name"/> </p> 

AngularJs ng-init directive

The ng-init directive initializes an AngularJS Application data.

 It is used to assign values to the variables.

In the following example, we initialize an array of countries. We use JSON syntax to define the array of countries.

<div ng-app="" ng-init="countries=[{locale:'en-US',name:'United States'},
 {locale:'en-GB',name:'United Kingdom'},
 {locale:'en-FR',name:'France'}]">

AngularJs ng-app directive

The ng-app directive starts an AngularJS Application.

 It defines the root element.

It automatically initializes or bootstraps the application when the web page containing AngularJS Application is loaded.

 It is also used to load various AngularJS modules in AngularJS Application.

 In the following example, we define a default AngularJS application using ng-app attribute of a
element. 

 <body ng-app="myapp">

</body? 

AngularJS directives

AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix. Let us discuss the following directives:

 ng-app - This directive starts an AngularJS Application.

 ng-init - This directive initializes application data.

 ng-model - This directive defines the model that is variable to be used in AngularJS.

 ng-repeat - This directive repeats HTML elements for each item in a collection.

Angularjs enter input field and display that field value

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script>
</head>
<body ng-app="">

<div>

<p> Enter Name : <input type="text" ng-model="name"/> </p>

<p> Name : <span ng-bind="name"/> </p>

</div>

</body>
</html>