AngularJS ng-init directive:-
The ng-init directive allows you to evaluate an expression in the current scope. In the following example, the ng-init directive initializes employees variable which is then used in the ng-repeat directive to loop thru each employee. In a real world application you should use a controller instead of ng-init to initialize values on a scope.
Demo.html
Name | Gender | City |
---|---|---|
{{ employee.name }} | {{ employee.gender}} | {{ employee.city}} |
ng-init should only be used for aliasing special properties of ng-repeat directive. In the following example, ng-init is used to store the index of the parent element in parentIndex variable.
Demo1.html
-
{{country.name}}
- {{city.name}} - Parent Index = {{ parentIndex }}, Index = {{ $index }}
Demo.js