AngularJS Get Started

Set up your first AngularJS page by loading the library and activating it with the ng-app directive.

Getting AngularJS Onto a Page

AngularJS is a plain JavaScript file. To use it, you only need to load it with a <script> tag before your own code runs — there is no compiler, bundler, or command-line tool required to get started.

  • Load it from a CDN (Content Delivery Network) with a <script src="..."> tag
  • Or download angular.min.js and reference it from a local folder
  • Add the ng-app directive to the HTML element you want AngularJS to control

Example

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>

<div ng-app="">
  <p>1 + 2 = {{ 1 + 2 }}</p>
</div>

</body>
</html>

The ng-app Directive Starts Everything

ng-app tells AngularJS which HTML element is the root of the application. AngularJS only looks for other directives — ng-model, ng-repeat, ng-bind, and so on — inside the element that carries ng-app. Anything outside it is treated as regular, inert HTML.

Example

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>

  <div ng-app="">
    <p>Inside the app: {{ 5 * 5 }}</p>
  </div>

  <p>Outside the app: {{ 5 * 5 }}</p>

</body>
</html>
Note: In the example above, the second {{ 5 * 5 }} is printed literally as text because it sits outside the ng-app element — AngularJS never sees it.

One Automatic Application Per Page

A page can only auto-bootstrap one ng-app element. If you need more than one independent application on the same page, you must start the extra ones manually with angular.bootstrap() in JavaScript, which is an advanced technique rarely needed for typical pages.

Setup MethodWhen To Use It
CDN <script> tagFastest way to start; no download needed
Local angular.min.js fileOffline development or strict network policies
ng-app on an elementMarks the root of the AngularJS application
Note: Keep the <script> tag for AngularJS in the <head>, or at least before any code that touches Angular — the library must be loaded before the browser parses the ng-app element.

Exercise: AngularJS Get Started

What does the ng-init directive do?