AngularJS Hello World: Your First Example Program
โก Smart Summary
AngularJS Hello World programs need only a single HTML file, one script reference and two directives, ng-app and ng-controller, to bind a controller scope variable into the page through a double-brace expression.
The best way to see the power of an AngularJS Application is to create your first basic program โHello Worldโ app in AngularJS.
There are many integrated development environments you can use for AngularJS development, some of the popular ones are mentioned below. In our example, we are using Webstorm as our IDE.
- Webstorm
- Sublime Text
- AngularJS Eclipse
- Visual Studio
Hello world, AngularJS
Version note: AngularJS 1.x left Long Term Support on 31 December 2021, and the framework team confirms that AngularJS support officially ended in January 2022, with 1.8.3 the final release and no security patches after it. Every step and code block below is preserved exactly as written. New projects belong in modern Angular, where the same exercise starts with ng serve and a standalone component and still uses the same {{ }} interpolation syntax.
The example below shows the easiest way to create your first โHello worldโ application in AngularJS.
The annotated screenshot below labels each part of that file, and its seven numbers match the Code Explanation list underneath. It was captured against an earlier 1.4.0 build, so its script line differs from the version pinned in the code.
<!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf 8"> <title>Guru99</title> </head> <body> <h1 ng-controller="HelloWorldCtrl">{{message}}</h1> <script src="https://code.angularjs.org/1.6.9/angular.js"></script> <script> angular.module("app", []).controller("HelloWorldCtrl", function($scope) { $scope.message="Hello World" } ) </script> </body> </html>
Code Explanation:
- The โng-appโ keyword is used to denote that this application should be considered as an AngularJS application. Any name can be given to this application.
- The controller is what is used to hold the business logic. In the h1 tag, we want to access the controller, which will have the logic to display โHello Worldโ, so we can say, in this tag we want to access the controller named โHelloWorldCtrlโ.
- We are using a member variable called โmessageโ which is nothing but a placeholder to display the โHello Worldโ message.
- The โscript tagโ is used to reference the angular.js script which has all the necessary functionality for AngularJS. Without this reference, if we try to use any AngularJS functions, they will not work.
- โControllerโ is the place where we are actually creating our business logic, which is our controller. The specifics of each keyword will be explained in the subsequent chapters. What is important to note is that we are defining a controller method called โHelloWorldCtrlโ which is being referenced in Step 2.
- We are creating a โfunctionโ which will be called when our code calls this controller. The $scope object is a special object in AngularJS which is a global object used within AngularJS. The $scope object is used to manage the data between the controller and the view.
- We are creating a member variable called โmessageโ, assigning it the value of โHello Worldโ and attaching the member variable to the scope object.
NOTE: The ng-controller directive is a keyword defined in AngularJS (Step 2) and is used to define controllers in your application. Here in our application, we have used the ng-controller keyword to define a controller named โHelloWorldCtrlโ. The actual logic for the controller will be created in (Step 5).
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
The message โHello Worldโ will be displayed.
How to Load AngularJS: CDN or Local File
The example above works because one line pulls the framework into the page before the browser reaches the end of the body. AngularJS ships as a single JavaScript file, so a first program needs no build step and no package manager. A plain HTML file opened in a browser is enough.
There are two ways to bring that file in.
- Reference a CDN copy. The example points at
code.angularjs.organd pins version 1.6.9. That host still serves 1.x builds, the highest being 1.8.2, while Google Hosted Libraries no longer lists AngularJS. - Download a local copy. Save the file beside the HTML page and point the script tag at it. A local copy keeps the example working offline, which matters when a proxy blocks the CDN.
<!-- Option 1: reference a CDN copy --> <script src="https://code.angularjs.org/1.8.2/angular.min.js"></script> <!-- Option 2: reference a downloaded copy in the same folder --> <script src="angular.min.js"></script>
Use angular.js while learning and angular.min.js in production: the unminified build prints readable stack traces, whereas the minified build reports renamed variables.
Once the file is loaded, ng-app starts the framework. AngularJS looks for that attribute when the document finishes loading, treats the element carrying it as the compilation root, and bootstraps the module named in its value. Three consequences follow:
- Only the first
ng-appon a page is bootstrapped automatically. A second one is ignored unlessangular.bootstrapis called manually. - The value must name a module registered with angular.module, otherwise the console reports
[$injector:modulerr]. - Placing
ng-appon thehtmlelement, as the example does, brings the whole document under AngularJS; a narrower container works too.
How {{ }} Expressions and ng-model Binding Work
The double braces around message in the h1 tag are not a placeholder filled once at load time. AngularJS compiles that text node into a watcher on the surrounding scope, so the rendered output tracks $scope.message for as long as the page lives.
The cycle runs in three steps.
- The controller writes a value onto
$scope. - A digest pass compares every watched expression with its previous result.
- Each expression whose value changed is re-rendered in place.
An interpolated expression is therefore one-way, from scope to view. Two-way binding needs ng-model, the directive designed for form controls. Add <input ng-model="message"> beside the heading, type into the box, and the heading changes on every keystroke, because ng-model writes the field value back onto the same scope property the expression watches.
Expressions are deliberately limited. They read scope properties, call scope methods and tolerate undefined values silently, yet they cannot declare variables, use control-flow statements or reach global objects such as window.
How to Fix a Blank Page or Unrendered {{ }}
A first AngularJS program fails in one of two visible ways: the page stays blank, or the literal text {{message}} appears where Hello World should be. Both mean the framework never took over the markup. Work through the checks below in order, with the browser console open.
- Confirm the library loaded. Open the Network panel and look for the
angular.jsrequest. A 404 or an unreachable CDN leaves the braces on screen untouched. - Check the script path. A relative path resolves against the folder holding the HTML file, so a copy in a
jssubfolder needsjs/angular.min.js. - Verify ng-app. A missing or misspelled attribute means nothing bootstraps. The attribute is hyphenated as
ng-appin markup, even though the directive is camel-cased in JavaScript. - Read the console error.
[$injector:modulerr]means the module named by ng-app was never registered.[ng:areq]usually means the controller name in the markup does not match the name passed to.controller(). - Look for a second ng-app. Only the first is bootstrapped, so a stray copy left on an inner element silently disables that part of the page.
Two further causes account for most of the remaining reports.
- The file protocol. This single-file example runs fine when opened directly from disk, but any template fetched over XHR is blocked by browser origin rules. Serve the folder over HTTP once templates appear.
- A brief flash of braces. When markup renders before the framework compiles it, the raw expression is visible for a moment. Add
ng-cloakto the element, or swap the expression forng-bind="message", which never displays its own markup.
If none of these apply, reduce the page to the example above and add your own code back one line at a time.


