Overview
In this article, we will see what the ng-init directive is in Angular JS. The ng-init directive allows you to evaluate the current expression in scope. We will see this with an example.
For more articles on AngularJS, kindly refer to
- Overview Of AngularJS
- Modules And Controller In AngularJS
- Controllers in AngularJS
- AngularJS ng src Directive
- AngularJS ng-repeat Directive
- Handling Events In AngularJS
- How To Use Two-Way Data Binding In AngularJS
- Filters In AngularJS
Introduction
Create a simple page.
<body>
<form id="form1" runat="server">
<div> </div>
</form>
</body>
</html>
In the div element, we are going to use the ng-init directive. We will create an expression, which will contain an array of employees.
<div ng-init="employees = [
{ name: 'Akshay', gender: 1, dateOfBirth: new Date('July 21, 1960'), Address: 'Mumbai', Salary: '1234.34' },
{ name: 'Milind', gender: 2, dateOfBirth: new Date('July 20, 1960'), Address: 'Nashik', Salary: '12.34' },
{ name: 'Raghvan', gender: 1, dateOfBirth: new Date('July 19, 1960'), Address: 'Pune', Salary: '1235.34' },
{ name: 'Hari', gender: 1, dateOfBirth: new Date('July 18, 1960'), Address: 'Mumbai', Salary: '5589.34' }
]">
</div>
As we included the ng-init directive in the div element, we don’t have to include it again. It will be reflected in child elements also.
In this, we will present the data in a table and the elements to display those header elements.
<table>
<thead>
<tr>
<th>Name</th>
<th>gender</th>
<th>dateOfBirth</th>
<th>Address</th>
<th>Salary</th>
</tr>
</thead>
</table>
In this, we will loop through those records in an array with the ng-repeat directive.
<tr ng-repeat="employee in employees"></tr>
We will use the binding expression to display the data.
<td>{{employee.name}}</td>
<td>{{employee.gender}}</td>
<td>{{employee.dateOfBirth}}</td>
<td>{{employee.Address}}</td>
<td>{{employee.Salary}}</td>
You can also write the employees in the controller file and specify the controller name in ng-init.
var mypartone = angular.module("mymodule", []).controller("myController", function($scope) {
var employees = [
{ name: "Akshay", gender: 1, dateOfBirth: new Date("July 21,1960"), Address: "Mumbai", Salary: "1234.34" },
{ name: "Milind", gender: 2, dateOfBirth: new Date("July 20,1960"), Address: "Nashik", Salary: "12.34" },
{ name: "Raghvan", gender: 1, dateOfBirth: new Date("July 19,1960"), Address: "Pune", Salary: "1235.34" },
{ name: "Hari", gender: 1, dateOfBirth: new Date("July 18,1960"), Address: "Mumbai", Salary: "5589.34" }
];
$scope.employees = employees;
});
Now, just add the controller name and model name.
<body ng-app="mymodule">
<div ng-init="myController">
You should always use a controller instead of ng-init to initialize the $scope.
Ng-init should be used for aliasing special properties of the ng-repeat directive.







Muhammad Aqib ShehzadPosted Jul 28, 2016, 8:49 AM
Good share
Vignesh ManiPosted Jul 28, 2016, 6:38 AM
Nice
Akshay PhadkePosted Jul 28, 2016, 1:36 AM
Prasanna MNarasimha Reddy Chennupalli Thankyou
Narasimha Reddy ChennupalliPosted Jul 28, 2016, 12:33 AM
Good one
Prasanna MuraliPosted Jul 27, 2016, 9:40 PM
Nice share