angular2 with type script

41
1 Angular2 with TypeScript - Ravi Mone

Upload: ravi-mone

Post on 16-Apr-2017

721 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Angular2 with type script

1

Angular2 with TypeScript

- Ravi Mone

Page 2: Angular2 with type script

About me

● I AM Ravi Mone● Have ample year of experience in back-end and Front-end. ● Working on technologies like Symfony2, AngularJS, ReactJS.● Currently serving in Techjini Solution as Team Leader.● You can connect me via

○ https://in.linkedin.com/in/ravi-mone-49b26519

Page 3: Angular2 with type script

Agenda

• What is TypeScript?• Why use TypeScript?• Angular2 Framework Architecture• What’s happening in Angular2?• Building Blocks of Angular2• Life Cycle Hooks• Bootstrap the application• Routing• Demo

Page 4: Angular2 with type script

What is TypeScript?

Page 5: Angular2 with type script

TypeScript (contd.)

Page 6: Angular2 with type script
Page 7: Angular2 with type script

Why use TypeScript? (Contd.)

• TypeScript follows a less radical/progressive approach.• It’s a typed superset of JavaScript and existing JavaScript projects can be

converted to TypeScript simply by renaming the source files from*.js to *.ts

Page 8: Angular2 with type script

Angular2 Framework Architecture

Page 9: Angular2 with type script

What’s happening in Angular2?

• Angular2 is not yet stable. The features and guidelines are subject to change from time to time.

• Current RC (Release Candidate) versionhttps://github.com/angular/angular/milestones

• To be aware of the weekly updates/modifications, visit: https://github.com/angular/angular/blob/master/CHANGELOG.md

• To know about the Angular2 Style Guide (Alpha-Version), visit: https://github.com/mgechev/angular2-style-guide

Page 10: Angular2 with type script

8 Main building blocks of an Angular2 App

1. Module2. Component3. Template4. Meta Data5. Data-Binding6. Services7. Directives8. Dependency Injection

Page 11: Angular2 with type script

Module (Export/Import)

• In ES6 each module is defined in its own file. • The functions or variables defined in a module are not visible outside

unless you explicitly export them. This means that you can write code in your module and only export those values which should be accessed by other parts of your app.

• ES6 modules are declarative in nature. To export certain variables from a module you just use the keyword export.

• Similarly, to consume the exported variables in a different module you use import.

Page 12: Angular2 with type script

Let’s create a simple module with two utility functions:

generateRandom() : Generates a random number.sum() : Adds two numbers.Next, let’s create a file named utility.js for the module:

Page 13: Angular2 with type script

And in your app.js

Page 14: Angular2 with type script

2. Component (@Component({ … }) )

• In Angular 2, Components are the main way we build and specify elements and logic on the page.

• Create reusable UI building blocks for an application.1. Each Angular component requires a single @Component. The @Component annotation

specifies when a component is instantiated, and which properties and hostListeners it binds to.

2. When a component is instantiated, it acts according to the encapsulation valueViewEncapsulation.native,

ViewEncapsulation.Emulated,

ViewEncapsulation.None.

3. All template expressions and statements are then evaluated against the component instance.

Page 15: Angular2 with type script

3. Template

• We define a Component's view with its companion template. • A template is a form of HTML that tells Angular how to render the

Component.• A template looks like regular HTML, with data/event binding properties

Page 16: Angular2 with type script
Page 17: Angular2 with type script

Sample Angular Template

Page 18: Angular2 with type script

4. @ Meta Data

• Metadata is data that describes other data. • Meta is a prefix that in most information technology usages means "an

underlying definition or description." • Metadata summarizes basic information about data, which can make

finding and working with particular instances of data easier.

Page 19: Angular2 with type script

So far Meta Data Classes

Page 20: Angular2 with type script

5. {{ DATA BINDING}}

Page 21: Angular2 with type script

Data Binding (contd.)

1. The "interpolation" displays the component's hero.name property value within the <div> tags.

2. The [hero] property binding passes the selectedHero from the parent HeroListComponent to the hero property of the childHeroDetailComponent.

Page 22: Angular2 with type script

Data Binding (contd.)

3. The (click) event binding calls the Component's selectHero method when the user clicks on a hero's name.

4. Two-way data binding is an important fourth form that combines property and event binding in a single notation using the ngModeldirective

Page 23: Angular2 with type script
Page 24: Angular2 with type script

6. Service

• If a piece of code is needed by many components in our application then create a single reusable service.

• When a component needs this service we can simply inject it (the service) using DI (@Injectable).

• A service is the mechanism used to share functionalities over Components (or with one Component if our app contains only one Component).

• Service is the best place from where we can bring our external Data to our app. Or do some repetitive task or calculations.

• Service can be shared between as many as Components we want.

Page 25: Angular2 with type script

7. @Directive

● A directive is simply a class with a specific Metadata (@Directive decorator)

● We have three kinds of directives:○ Components: yes a component is a directive. (@Component)○ Structural directives: conditionally add or remove content from the

DOM.○ Attribute directives: Alters the Element by changing its behavior or the

appearance

Page 26: Angular2 with type script

Three Kinds of Directives

1. Component<angular-2-hello-world>loading…</angular-2-hello-world>

2. Structural Directive (ngIf, ngFor )<div*myAngular2Directive=”ShowMeIfFalse”> <b>I’m visible => showMeIfFalse=false </b></div>

3. Attribute Directive<p [zoomIn]=”blue”> Some thing goes here </p>

Page 27: Angular2 with type script

Directive Vs. Component

Page 28: Angular2 with type script

8. Dependency Injection (Provider, BootStrap(‘’, []))

• In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies.

• The idea behind dependency injection is very simple. If you have a component that depends on a service. You do not create that service yourself. Instead, you request one in the constructor, and the framework will provide you one. By doing so you can depend on interfaces rather than concrete types. This leads to more decoupled code, which enables testability, and other great things

Demo: http://plnkr.co/edit/CG9HuLzI6KqncxiVeACM?p=preview

Page 29: Angular2 with type script

Life Cycle Hooks

• When the component class implements some lifecycle_hooks the callbacks are called by the change detection at defined points in time during the life of the component.

Demo: http://plnkr.co/edit/IVn4bb4Fp2eDDPrLODay?p=preview

Page 30: Angular2 with type script

Bootstrap the App

● You instantiate an Angular application by explicitly specifying a component to use as the root component for your application via:○ bootstrap(‘<root component>’) in case there is a single component ○ bootstrap(‘<root component>’, [<DI>]) in case the component has

dependencies.

Page 31: Angular2 with type script

Routing

Page 32: Angular2 with type script
Page 33: Angular2 with type script
Page 34: Angular2 with type script
Page 35: Angular2 with type script
Page 36: Angular2 with type script
Page 37: Angular2 with type script
Page 38: Angular2 with type script
Page 39: Angular2 with type script

Demo Links• Kick Start: Demo a small component (meet-up folder)

• Forms: http://plnkr.co/edit/aN3LhpwnH2qyRYNfJn8q?p=preview

• Inputs: http://plnkr.co/edit/rV5s4CKZWLfhW63gGnKp?p=preview

• Outputs: http://plnkr.co/edit/7ByGLXviLy4SbrXgdlx7?p=preview

• ViewChild: http://plnkr.co/edit/vhdL7e0SccIbMk0eZKfh?p=preview

• Encapsulation : https://plnkr.co/edit/inF7VaYJvj8uJfuLKV4Z?p=preview

• Directives : http://plnkr.co/edit/P0G8cWYCP8sC7Yrvlo8L?p=preview

• CRUD application : https://github.com/ravi-mone/angular2-crud

• Routes : https://github.com/ravi-mone/angular2-lab

Page 40: Angular2 with type script
Page 41: Angular2 with type script