Angular 2 - SPLessons

Angular 2 Architecture

Home > Lesson > Chapter 2
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Angular 2 Architecture

Angular 2 Architecture

shape Introduction

Mobile and web applications can be developed using angular platform. The Angular 2 is not only an update for Angular 1.x but also have many breaking changes and had been re-written completely in TypeScript in order to meet the ES 6 specification. Following are some of the major topics covered.
  • Coding Angular 2

Coding Angular 2

shape Description

Angular is a framework for building consumer applications in hypertext markup language and either JavaScript or a language like typescript that compiles to JavaScript. The framework consists of many libraries, a number of them core and a few elective. Write the Angular applications by composing hypertext markup language templates with Angularized markup, writing component classes to manage those templates, adding application logic in services, and boxing components and services in modules. Then launch the app by bootstrapping the root module. Angular takes over, presenting app content in a browser and responding to user interactions in keeping with the directions provided. The figure below demonstrate the Angular 2 Architecture. Every Angular 2 application contain 8 main blocks as follows.

Module

shape Description

Module is that the block of code that is intended to perform one task. Developers can export the module in sort of class. Angular 2 applications have one or additional modules. Each Angular application should have a minimum of one module. If Angular application contains just one module, it's referring as root module. Each Angular application has one root module and lots of additional featured modules. Angular module is a class that is decorated with @NgModule. NgModule takes one metadata object and its properties describe the module. Following are the necessary properties of NgModule.

shape Example

The snippet code below is the simple root module of an Angular 2 application. app.module.ts [c] import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { } [/c]

Component

shape Description

A component is a controller class with a template that mainly deals with a view of the application and logic on the page. It’s a trifle of code which will be used throughout an application. Component is aware of the way to render itself and configure dependency injection. One can associate CSS styles using component inline styles, style urls and template inline styles to a component. Angular creates and updates the desired component and destroys the unused component as the user moves through an application.

shape Example

The snippet code below is the simple Component example of an Angular 2 application. app.component.ts [c] import { Component } from '@angular/core'; @Component({ selector: 'pm-app', template: `<h1>Angular2: Getting Started</h1>` }) export class AppComponent { } [/c]

Metadata

shape Description

Metadata is a manner of processing the class. Consider one component which can be a class till one tend to tell Angular that it is a component. One can use metadata to the class to inform Angular that it is a component. The metadata are often attached to typescript by using the decorator.

shape Example

The snippet code below is the simple Metadata example of an Angular 2 application. app.component.ts [c] @Component({ selector: 'pm-app', template: `<h1>Angular2: Getting Started</h1>` } [/c] The @Component decorator takes a needed configuration object with the information Angular needs to create and present the component and its view. Following are the configuration options of @Component.

Template

shape Description

The template is the component view which suggest Angular the way to show the component. As an example, below simple example shows how to display the name. [html] <div> product name : { {name} } </div> [/html] Put the template expression inside the interpolation braces in order to display the value.

Data Binding

shape Description

Data binding is a powerful feature of software development technologies. Data bind is that the connection bridge between view and also the business logic (View Model) of the application. Data binding in AngularJS is the automatic synchronization between the Model and view. Following are the four types of Bindings supported by Angular 2 applications.

Services

shape Description

Service in AngularJS is a function or an object that may be used to share the data and also the behavior across the application. It’s JavaScript function that is used to perform a specific task. It includes the function, values, or the other feature needed by the application. Typical samples of services are as follows

Directives

shape Description

Directives are one in all the most necessary components of AngularJS application. They’re extended html attributes. These are the markers on the DOM element that provides some special behavior to DOM elements and tell Angular's html compiler to connect. It’s category with directive metadata to make directive, developer need to use @Directive decorator on attached metadata to the class. Following are the three types of directives in Angular 2.

Dependency Injection

shape Description

Dependency Injection could be a design pattern that passes an object as dependencies in numerous components across the application. It creates new an instance of class along with its required dependencies. Following are few points to remember about the Dependency injection.

Summary

shape Key Points

  • Angular is a framework for building client applications in HTML.
  • Module is the block of the code which is intended to perform single task.
  • The component view is suggested to angular by template.
  • Data binding is a powerful feature of software development technologies.
  • Service in AngularJS is a function or an object that may be used to share the data.