AngularJS ng-app Directive

Definition and Usage

ng-app directive tells AngularJS that this is the root element of an AngularJS application.

All AngularJS applications must have a root element.

Only one in the HTML document can be used ng-app directive. If there are multiple ng-app If multiple directives are used, the first directive encountered will be used.

Instance

Example 1

Make the body element the root element of an AngularJS application:

<body ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</body>

Try It Yourself

Example 2

Load modules to run in the application:

<div ng-app="myApp" ng-controller="myCtrl">
    {{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "Bill";
    $scope.lastName = "Gates";
});
</script>

Try It Yourself

Syntax

<element ng-app="modulename">
...
  The content within the ng-app root element can contain AngularJS code
...
</element>

Supported by all HTML elements.

Parameters

Parameters Description
modulename Optional. Specify the name of the module to be loaded with the application.