AngularJS Techniques

Start Working With AngularJS

AngularJS is a Javascript MVC framework produced by Google to build properly architectured and maintenable web applications.

AngularJS takes another approach. It attempts to minimize the impedance mismatch between document centric HTML and what an application needs by creating new HTML constructs. AngularJS teaches the browser new syntax by a construct we call directives. Examples include:

Data binding, as in {{}}.

DOM control structures for repeating, showing and hiding DOM particles.

sustain for forms and form validation.

Attaching new behavior to DOM elements, such as DOM event handling.

Grouping of HTML into reusable elements.

Why AngularJS?

AngularJS is a MVC framework that defines numerous concepts to properly organize our web application. Our application is defined with modules that can depend from one to the others. It enhances HTML by attaching directives to your pages with new attributes or tags and expressions in order to define very powerful templates directly in your HTML. It also encapsulates the behavior of your application in controllers which are instanciated thanks to dependency injection. Thanks to the use of dependency injection, AngularJS helps you structure and test your Javascript code very easily. Finally, utility code can easily be factorized into sets that can be injected in your controllers. Now let’s have a closer look at all those features.

characterize 1: Two Way Data-Binding

Think of your form as the single-source-of-truth for your application. Your form is where you go to to read or update anything in your application.

Data-binding is probably the coolest and most useful characterize in AngularJS. It will save you from writing a important amount of boilerplate code. A typical web application may contain up to 80% of its code base, dedicated to traversing, manipulating, and listening to the DOM. Data-binding makes this code disappear, so we can focus on our application.

Think of your form as the single-source-of-truth for your application. Your form is where you go to to read or update anything in your application. The data-binding directives provide a projection of your form to the application view. This projection is seamless, and occurs without any effort from you.

Traditionally, when the form changes, the developer is responsible for manually manipulating the DOM elements and attributes to mirror these changes. This is a two-way street. In one direction, the form changes excursion change in DOM elements. In the other, DOM component changes necessitate changes in the form. This is further complicated by user interaction, since the developer is then responsible for interpreting the interactions, merging them into a form, and updating the view. This is a very manual and cumbersome course of action, which becomes difficult to control, as an application grows in size and complexity.

There must be a better way! AngularJS’ two-way data-binding handles the synchronization between the DOM and the form, and vice versa.

Here is a simple example, which demonstrates how to bind an input value to an

component.

Name:

go into a name here

Hello, {{yourName}}!

This is extremely simple to set up, and almost magical…

characterize 2: Templates

It’s important to realize that at no point does AngularJS manipulate the template as strings. It’s all the browser DOM.

In AngularJS, a template is just plain-old-HTML. The HTML vocabulary is extended, to contain instructions on how the form should be projected into the view.

The HTML templates are parsed by the browser into the DOM. The DOM then becomes the input to the AngularJS compiler. AngularJS traverses the DOM template for rendering instructions, which are called directives. Collectively, the directives are responsible for setting up the data-binding for your application view.

It is important to realize that at no point does AngularJS manipulate the template as strings. The input to AngularJS is browser DOM and not an HTML string. The data-bindings are DOM transformations, not string concatenations or inner HTML changes. Using the DOM as the input, instead of strings, is the biggest differentiation AngularJS has from its sibling frameworks. Using the DOM is what allows you to extend the directive vocabulary and build your own directives, or already recondite them into reusable elements!

One of the greatest advantages to this approach is that it creates a tight workflow between designers and developers. Designers can mark up their HTML as they typically would, and then developers take the baton and hook in functionality, via bindings with very little effort.

Here is an example where I am using the ng-repeat directive to loop over the images range and populate what is essentially an img template.

function AlbumCtrl($scope) {

scope.images = [

{“thumbnail”:”img/image_01.png”, “description”:”Image 01 description”},

{“thumbnail”:”img/image_02.png”, “description”:”Image 02 description”},

{“thumbnail”:”img/image_03.png”, “description”:”Image 03 description”},

{“thumbnail”:”img/image_04.png”, “description”:”Image 04 description”},

{“thumbnail”:”img/image_05.png”, “description”:”Image 05 description”}

];

}

{{image.description}}

It is also worth mentioning, as a side observe, that AngularJS does not force you to learn a new syntax or extract your templates from your application.

characterize 3: MVC

AngularJS incorporates the basic principles behind the original MVC software design pattern into how it builds client-side web applications.

The MVC or form-View-Controller pattern method a lot of different things to different people. AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (form-View-ViewModel).

The form

The form is simply the data in the application. The form is just plain old JavaScript objects. There is no need to inherit from framework classes, wrap it in proxy objects, or use special getter/setter methods to access it. The fact that we are dealing with vanilla JavaScript is a really nice characterize, which cuts down on the application boilerplate.

The ViewModel

A viewmodel is an object that provides specific data and methods to continue specific views.

The viewmodel is the $scope object that lives within the AngularJS application. $scope is just a simple JavaScript object with a small API designed to detect and broadcast changes to its state.

The Controller

The controller is responsible for setting initial state and augmenting $scope with methods to control behavior. It is worth noting that the controller does not store state and does not interact with far away sets.

The View

The view is the HTML that exists after AngularJS has parsed and compiled the HTML to include rendered markup and bindings.

This division creates a substantial foundation to architect your application. The $scope has a reference to the data, the controller defines behavior, and the view handles the layout and handing off interaction to the controller to respond consequently.

characterize 4: Dependency Injection

AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test.

Dependency Injection (DI) allows you to ask for your dependencies, instead of having to go look for them or make them yourself. Think of it as a way of saying “Hey I need X’, and the DI is responsible for creating and providing it for you.

To gain access to chief AngularJS sets, it is simply a matter of adding that service as a parameter; AngularJS will detect that you need that service and provide an example for you.

function EditCtrl($scope, $location, $routeParams) {

// Something clever here…

}

You are also able to define your own custom sets and make those obtainable for injection in addition.

angular.

module(‘MyServiceModule’, []).

factory(‘notify’, [‘$window’, function (win) {

return function (msg) {

win.alert(msg);

};

}]);

function myController(scope, notifyService) {

scope.callNotify = function (msg) {

notifyService(msg);

};

}

myController.$inject = [‘$scope’, ‘notify’];

characterize 5: Directives

Directives are my personal favorite characterize of AngularJS. Have you ever wished that your browser would do new tricks for you? Well, now it can! This is one of my favorite parts of AngularJS. It is also probably the most challenging aspect of AngularJS.

Directives can be used to create custom HTML tags that serve as new, custom widgets. They can also be used to “decorate” elements with behavior and manipulate DOM attributes in interesting ways.

Here is a simple example of a directive that listens for an event and updates its $scope, consequently.

myModule.directive(‘myComponent’, function(mySharedService) {

return {

restrict: ‘E’,

controller: function($scope, $attrs, mySharedService) {

$scope.$on(‘handleBroadcast’, function() {

$scope.message = ‘Directive: ‘ + mySharedService.message;

});

},

replace: true,

template: ‘



};

});

Then, you can use this custom directive, like so.

Creating your application as a composition of discrete elements makes it incredibly easy to add, update or delete functionality as needed.

we will discuss here about how to set up AngularJS library to be used in web application development. We will also briefly study the directory structure and its contents.

When you open the link https://angularjs.org/, you will see there are two options to download AngularJS library –

AngularJS Download

View on GitHub – Click on this button to go to GitHub and get all of the latest scripts.

Download AngularJS 1 – Or click on this button, a screen as below would be seen –

AngularJS Download

This screen gives various options of using Angular JS as follows –

Downloading and hosting files locally

There are two different options legacy and latest. The names itself are self descriptive. legacy has version less than 1.2.x and latest has 1.5.x version.

We can also go with the minified, uncompressed or zipped version.

CDN access – You also have access to a CDN. The CDN will give you access around the world to regional data centers that in this case, Google great number. This method using CDN moves the responsibility of hosting files from your own servers to a series of external ones. This also offers an advantage that if the visitor to your webpage has already downloaded a copy of AngularJS from the same CDN, it won’t have to be re-downloaded.

Try the new angularJS 2 – Click on this button to download Angular JS beta 2 version.This version is very fast, mobile supported and flexible compare to legacy and

latest of AngularJS 1

We are using the CDN versions of the library throughout this tutorial.

Example

Now let us write a simple example using AngularJS library. Let us create an HTML file myfirstexample.HTML as below –

Welcome {{helloTo.title}} to the world of Tutorialspoint!

Following sections describe the above code in detail –

Include AngularJS

We have included the AngularJS JavaScript file in the HTML page so we can use AngularJS –

If you want to update into latest version of Angular JS, use the following script source or else Check the latest version of AngularJS on their official website. Point to AngularJS app Next we tell what part of the HTML contains the AngularJS app. This done by adding the ng-app attribute to the root HTML component of the AngularJS app. You can either add it to HTML component or body component as shown below –

View

The view is this part –

Welcome {{helloTo.title}} to the world of Tutorialspoint!

ng-controller tells AngularJS what controller to use with this view. helloTo.title tells AngularJS to write the “form” value named helloTo.title to the HTML at this

location.

Controller

The controller part is –

This code registers a controller function named HelloController in the angular module named myapp. We will study more about modules and controllers in their respective chapters. The controller function is registered in angular via the angular.module(… ).controller(… ) function call.

The $scope parameter passed to the controller function is the form. The controller function adds a helloTo JavaScript object, and in that object it adds a title field.

Execution

Save the above code as myfirstexample.HTML and open it in any browser. You will see an output as below –

Welcome AngularJS to the world of Tutorialspoint!

When the page is loaded in the browser, following things happen –

HTML document is loaded into the browser, and evaluated by the browser. AngularJS JavaScript file is loaded, the angular global object is produced. Next, JavaScript which registers controller roles is executed.

Next AngularJS scans by the HTML to look for AngularJS apps and views. Once view is located, it connects that view to the corresponding controller function.

Next, AngularJS executes the controller roles. It then renders the views with data from the form populated by the controller. The page is now ready.

Leave a Reply