Over a million developers have joined DZone.

AngularJS - My First Hello World Tutorial

DZone's Guide to

AngularJS - My First Hello World Tutorial

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

I have been using AngularJS for a while now and decided to write a series of post on AngularJS. I will start with a basic Hello World post which mostly focuses on how to setup the environment for AngularJS to use it. Later I will gradually cover up all (or most of) the important concepts on AngularJS and then we will see how to integrate AngularJS with other tools and frameworks (like Spring MVC, Laravel, etc).

What is AngularJS ?

AngularJS is an open-source web-based JavaScript framework developed to make easier to implements Single Page Application (SPA). It’s developed and maintained by Google and community of developers and the goal is to simplify development and testing of client side web application.

AngularJS Hello World

Follow the simple steps to create your first AngularJS application.

Step 01)

First step is to include angularjs javascript file into your HTML document. You can download the complete AngularJS from https://angularjs.org site but for this tutorial I recommend you to use CDN url as below.

<!DOCTYPE html>
<html lang="en">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>


Step 02)

Second step is to create an angular module and define a Controller. The job of controller is to control the information that we put on page or in editing scenario controller save the information that use is typing into the page.

I am creating a separate javascript file and add the angular code.

var app = angular.module("app", []);
app.controller("HelloController", function($scope) {
  $scope.message = "Hello, AngularJS";	

As you can see in about code snippet, I called module() function of angular global variable and pass the module name. This module has function call controller() which we can use to define our firstHelloController.

As you can see in above code snippet, controllers are just functions. We don’t invoke this functions but angular will call this controller function when it need to manage the section of HTML page which has ng-controller directive. When Angular create controller, it pass the parameter to function called $scope. We can attached our models or data to this $scope variable.

Step 03)

To use the controller on page, we need to use a directive called ng-controller. Its an attribute which place in HTML and used to controller some area in HTML document.

<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src="resources/js/angular.js"></script>
<script type="text/javascript" src="resources/js/script.js"></script>
<body ng-app="app">
	<div ng-controller="HelloController">

Remember, $scope is not the model but the things that we attached to it is the model. In the above code snippet we only attached a single property to the $scope called message which points to a string value. This one will make message available to us inside HTML so we can data bind into display.


I hope you enjoy the post, there are lot of post on AngularJS to come in future so stay tuned.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Published at DZone with permission of Tousif Khan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}