Angular 2 Building Blocks
Angular 2 Building Blocks
What is Angular 2?
It is a JavaScript framework for creating web and mobile applications.
It supportsTypeScript, a super script of Javascript that helps in safe coding.
It has several enhancements on top of Angular 1.x, which makes it simple to use and
get the desired output. But knowledge in Angular 1.x is not necessary to learn
Angular 2, since the whole framework is re-written.
Why Angular 2?
Easy: Unlike previous versions, Angular 2 focuses only on building JavaScript
classes; hence it is easier to learn.
Freedom: It provides more choices of languages for consumption i.e. ES5, ES6,
Typescript, and Dart. TypeScript is recommended here.
Flexible: It is a Cross-platform framework.
Faster: Because of server side rendering.
Simpler: Component based approach with no controllers and $scope.
Performance: Uses Hierarchical Dependency Injection system, which is a performance
booster.
What is Module?
What is Module?
A Module is a way of organizing related Components, Services, Directives, and Pipes
with a specific functionality.
There can be several Modules within an app, but it should consist of at least one
root module. Root Module, by convention, must be named: AppModule.
Module - Example
This is a sample code for module.
What is Component?
A component is the basic block of angular 2 app.
It handles the view part of the app and has the core logic of the page.
It can render the view by itself based on the dependency Injection.
There can be only one component per DOM element (i.e. only one selector element).
Element's behavior can be manipulated by the properties associated with
corresponding component.
It can be identified using the decorator @Component.
Components can be created, updated or destroyed based on the user actions on the
page.
Components - Example
This example will render Hello, World! in the browser. Angular will search for <my-
app> element in HTML file and replace it with the template content.
File: app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<p>Hello, World!</p>',
})
export class AppComponent{}
File: index.html
............
<body>
<my-app> Loading... </my-app>
</body>
............
You can check out the complete code in Plunker here
What is Template?
Template is a simple HTML code that creates view for a component, which you can
dynamically manipulate.
template
templateUrl
What is Metadata?
In Angular 2, Metadata is vital in getting the code processed and shown on the
screen. Metadata means data of data, which gives more info about data.
Metadata - Example
Let us consider a simple example code as shown below. Suppose you want to have a
component, Angular will consider it as a simple "class" until you explicitly use
"Decorator" and attach metadata to TypeScript.
There are four ways you can bind a data to the DOM depending on the direction the
data flows.
Interpolation
Interpolation acts as local variable, which allows us to bind data from "Component
Class" onto your "Template".
It is defined using {{ }} double curly braces.
If we consider an example, 20 + 20 results in {{20 + 20}}, the actual mathematical
calculation is done and output is displayed as 20 + 20 results in 40.
It can also access values from component class. Ex - {{myName}}
It can further assign attributes to the tags. Ex - <img src = {{myImageURL}}>
Interpolation Demo
import { component } from `@angular2/core`;
@Component ({
selector: 'myApp',
template: `
<h1> {{title}} </h1> //Interpolation Binding `
})
export class AppComponent {
title: "Hello Fresco !"
}
Property Binding Demo
import { component } from `@angular2/core`;
@Component ({
selector: 'myApp',
template: `
<h1 [innerHtml] = "title"> </h1> //Property Binding
`
})
export class AppComponent {
title: "Hello Fresco !"
}
Two-way data binding is a combination of both Property Binding and Event Binding
allowing us to update data in both directions. It is defined using [( )] popularly
known as banana brackets.
Structural Directive
They manipulate the DOM elements. i.e. Delete or update the elements. This changes
the structure of webpage.
Some of the built-in structural directives, Angular 2 offers are NgIf, NgFor and
NgSwitch.
Attribute Directive
These directives add styling by manipulating the Attributes of DOM element. They do
not change the structure of webpage.
NgStyle and NgClass are some of the built-in attribute directives, Angular 2
provides.
Do You Know?
Technically Components is a subset of directives except the fact that they have a
mandatory template parameter.
What is Service?
Services are functions that are designed to complete a specific task. If you want
to bring external data from server into your app or perform some repetitive task,
then services come handy.
@Injectable()
export class MyService {
addFunction(a,b){ return a+b; }
}
Angular finds which services are required to the component, by looking at the
"type" of component's constructor parameters.
Angular then checks for the service in Injector, which makes a container of all
services that have already been created.
You will learn more about Services and Dependency Injection in detail in a
different course.
Create a page as shown in the picture. Have the following elements in your app.
Header
Label – Display ONLY IF there are no activities
Text Input box
Button 1 – To add an activity
Button 2 – To clear all activities