Over a million developers have joined DZone.

AngularJS - My First Hello World Tutorial

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>
<body>

</body>
</html>

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">
<head>
<title>AngularJS</title>
<script type="text/javascript" src="resources/js/angular.js"></script>
<script type="text/javascript" src="resources/js/script.js"></script>
</head>
<body ng-app="app">
	<div ng-controller="HelloController">
		<h2>{{message}}</h2>
	</div>
</body>
</html>

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.

Output

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}