Wednesday, February 11, 2015

ng-init directive

The ng-init directive, is used to initialize specific variables and use them within expressions. It acts more like a Declare statement where we can specify name/value pairs of variables. Before starting with any AngularJs example make sure you download and refer to a local copy of the Angularjs library file, or refer to one of the online libraries for AngularJS.

Following is a simple example of using ng-init directive to declare a Name variable and assign a value to it.


<html ng-app>
<head>
    <meta charset="utf-8" />
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
</head>
<body>
<div ng-init="Name='Peter'">
<p>Your name is :<b> {{ Name }} </b></p>
</div>
</body>
</html>

Output:



The ng-init directive can also be used to initialize an array of values as follows

Search Flipkart Products:
Flipkart.com

No comments: