AngularJS ng-init



  • Ng-init directive is used to define the initial value of the application data.
  • Instead of using ng-init, we will use controllers to initial the values.

Syntax for ng-init directive in AngularJS:

<element ng-init="expression" ></element>

Parameter Values:

Value Description
expression Denotes an expression to evaluate.

Sample code for ng-init directive in AngularJS:

 Tryit<!DOCTYPE html>
<html>
    <head>
        <title>Wikitechy AngularJS Tutorials</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/
        angular.min.js"> </script>
    </head>
    <body ng-app="" ng-init="myText='Welcome to Wikitechy.com'">
        <h2>{{ myText }}</h2>
    </body>
</html>

ng-init directive in AngularJS:

<div ng-app="" ng-init="myText='Welcome to Wikitechy.com'">
    <h2>>{{ myText }}></h2>
</body>

Code Explanation for ng-init directive in AngularJS:

Code Explanation for ng init Directive In AngularJS

  1. The <script> tag is used to include AngularJS JavaScript source file from googleapis.com into HTML document.
  2. The ng-app specifies the root element (e.g. <body> or <html> or <div> tags) to define AngularJS application.
  3. In AngularJS applications ng-init is used to initialize the value for expression in the current scope.

Sample Output for ng-init directive in AngularJS:

Sample Output for ng init Directive In Angularjs

  1. The value Welcome to Wikitechy.com which is given within the ng-init directive is displayed in the output.



Related Searches to angularjs ng-init directive