EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials AngularJS Tutorial AngularJs ng init
Secondary Sidebar
AngularJS Tutorial
  • AngularJS Basic and Advance
    • What is AngularJS
    • Career In AngularJS
    • Uses of Angular JS
    • AngularJS Architecture
    • Angular JS Application
    • AngularJS Application
    • AngularJS Filter Array
    • AngularJS ng-style
    • AngularJs ng init
    • AngularJs ng-repeat
    • AngularJs ng-change
    • AngularJs ng-options
    • AngularJs expressions
    • AngularJs onclick
    • AngularJS ng-show
    • AngularJs ng-include
    • Scope in AngularJS
    • AngularJS ng-if
    • AngularJS Number Filter
    • AngularJS Date Filter
    • AngularJS ng-class
    • AngularJS ng-model
    • AngularJS ng-disabled
    • AngularJS Custom Filter
    • AngularJS Validation
    • AngularJS Filters
    • Controllers in AngularJS
    • AngularJS Animations
    • AngularJS Services
    • AngularJS Events
    • AngularJS Versions
    • AngularJS Directives
    • AngularJS Unit Testing
    • AngularJS with Bootstrap
    • AngularJS UI Bootstrap
    • AngularJS Routing Example
  • Interview Question
    • Angular JS Interview Questions

AngularJs ng init

AngularJs ng init

Introduction to AngularJs ng init

AngularJS ng-init is an In-Built AngularJS directive which is used in the HTML view for initializing variable within a particular scope ONLY instead of initializing and modifying it in the controller or the component. ng-init allows the user to evaluate a particular expression in the current scope in the HTML view. This can be used with any HTML tag such as div, select, input, span, etc. Mostly this directive is used to add a special additional property to the ng-repeat elements only for limited scope in the HTML view.

Syntax

There are various ways to write ng-init directive with different HTML Tags

Using ng-init with input type

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

<input type=”checkbox” ng-init=”anyExpression” ng-model=”modelValue”></input>

Using ng-init with a select type

<select ng-init=”anyExpression” ng-options=”reason as reason.desc for reason in reasonsList”></select>

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,502 ratings)

Using ng-init with div/span

<div ng-init=”anyExpression” ng-click=”onClickOperation”> Update Request </div?

the ng-init directive can be used with any  HTML element, and its scope is limited only within that element scope

How does the ng init directive work in AngularJS?

In the AngularJS framework, it is important to know that all the In-Built directives that the AngularJS framework has provisioned will always be denoted with the ng prefix.

The ng-init directive is for initializing variables within a particular scope ONLY in the HTML view instead of initializing and modifying it in the controller or the component. ng-init allows the user to evaluate a particular expression in the current scope in the HTML view. The way the ng-init directive works is as soon as the HTML view comes across the ng-init directive declared in any HTML elements, it evaluates the expression and variable defined in the ng-init directive, and that variable can be further used in that scope of HTML view. It is strictly recommended not to use ng-init for complex and advanced logic in the HTML template as for such operation, controller and components can be used, and that is considered an ideal way. The usage of ng-init is limited to places where local variable aliasing is needed in the HTML template or adding any special property to the ng-repeat directive or ng-options. It can be used when you need a particular variable to be initialized only during the development cycle, or you need to inject data through server-side scripting.

The ng-init directive is executed at priority level 450, and it can also be used along with filters in AngularJS. Therefore, the syntax for using ng-init along with the filter should be proper.

Example of AngularJs ng init

Given below are the example of AngularJs ng init:

Index.html

<!doctype html>
<html ng-app="initApp">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-controller="initController">
<div ng-init="amount1 = 100">Amount1: {{amount1 | currency:"USD$"}}</div><br/>
<span>Enter Amount: <input ng-model="amount1"/></span><br> <br>
<input type="checkbox" ng-model="mainCheckBox"
ng-init="mainCheckBox = true"/>CheckBox selected on init <br><br>
<div ng-repeat="adult in adultsList" ng-init="adultIndex = $index">
Adult index is {{adultIndex}} <br>
Name - {{adult.name}} <br>
Age - {{adult.age}} <br> <br>
</div>
</div>
</body>
</html>

Script.js

angular.module('initApp', []);
function initController($scope) {
$scope.amount1 = 1234.56;
$scope.adultsList = [{'name': 'Jian', 'age': 22}, {'name': 'Nobita', 'age': 26}];
}

The above example shows different ways of using the ng-init directive in the HTML view in an AngularJS application. We have taken various HTML elements to showcase the use of the ng-init directive in the HTML template. This directive is used in an HTML div element, where the initial amount value is set to 100, and if you see this ng-model is initialized in the controller as well, but HTML ng-init takes priority over controller value, and 100 is set as the initial Amount, and the same is displayed in the input field as well, as same ng-model value is used. If the model value is updated in the input type, it will reflect and override the init value. Another example is using ng-init with input type checkbox, where the requirement is that when the page loads for the first time, then this checkbox should be selected, so the ng-init is used here where the model value of the checkbox is set to TRUE on init, which default the checkbox to selected every time the page is loaded. Later on, this checkbox can be selected or deselected, and the value will get updated accordingly. The third example shows how in ng-repeat or select ng-options directive ng-init can be used to keep track of $index, which signifies the index of each element in the Array list. This variable can be used inside the ng-repeat tag, and the scope is limited only till that it cannot be accessed outside the ng-repeat scope.

<div ng-init="amount1 = 100">

In this div block, the model value amount is initialized to 100 as the default init value.

<input ng-model="amount1"/>

Here the amount model value is binded to the input type, and if the input value is updated in the HTML template, then the model value will also get updated.

<input type="checkbox" ng-model="mainCheckBox" ng-init="mainCheckBox = true"/>

In this input type checkbox, the init value is set to True so that the first time the page loads, the checkbox is always selected.

<div ng-repeat="adult in adultsList" ng-init="adultIndex = $index">

Adult index is {{adultIndex}} <br>
Here ng-init is used with ng-repeat directive where the $index is tracked in a local variable named adultIndex, and this variable is used further to denote the adult index in the adult list.

Output:

Output 1: When the page is loaded for the first time

AngularJs ng init output 1

Output 2: When the amount of value is updated

AngularJs ng init output 2

Output 3: When the checkbox is modified

AngularJs ng init output 3

Conclusion

ng-init is a directive in AngularJS that comes with limited and restricted use where it can be used whenever there is aliasing of special property is required in ng-repeat or ng-options tag, evaluating data only during the development phase and you need to keep track of it. Other than these, it is highly recommended to use and evaluate expressions and variables in controllers and using ng-controller in angularJS. There are various forms of using this directive in different HTML tags, and with filters, the proper syntax is all that is needed, and you are ready to go.

Recommended Articles

This is a guide to AngularJs ng init. Here we discuss How does the ng-init directive work in AngularJS and Example along with the output. You may also have a look at the following articles to learn more –

  1. AngularJS Services
  2. Career In AngularJS
  3. AngularJS Alternatives
  4. AngularJS Architecture
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more