SlideShare a Scribd company logo
Angular2 tutorial
Angular 2
i
AbouttheTutorial
Angular 2 is an open source JavaScript framework to build web applications in HTML and
JavaScript, and has been conceived as a mobile first approach.
Audience
This tutorial is designed for software professionals who want to learn the basics of
AngularJS 2 and its programming concepts in simple and easy steps. It describes the
components of AngularJS 2 with suitable examples.
Prerequisites
You should have a basic understanding of JavaScript and any text editor. As we are going
to develop web-based applications using Angular 2, it will helpful if you have an
understanding of other web technologies such as HTML, CSS, AJAX, AngularJS, etc.
Copyright&Disclaimer
 Copyright 2016 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
Angular 2
ii
TableofContents
About the Tutorial....................................................................................................................................i
Audience..................................................................................................................................................i
Prerequisites............................................................................................................................................i
Copyright & Disclaimer.............................................................................................................................i
Table of Contents....................................................................................................................................ii
ANGULAR 2 — OVERVIEW...................................................................................................1
ANGULAR 2 — ENVIRONMENT............................................................................................3
Creating Configuration Files ....................................................................................................................3
Creating Our First Angular Component ...................................................................................................6
Compile and Run.....................................................................................................................................8
ANGULAR 2 — HELLO WORLD.............................................................................................9
ANGULAR 2 — ARCHITECTURE..........................................................................................13
Module .................................................................................................................................................14
Component ...........................................................................................................................................14
Template...............................................................................................................................................14
Metadata ..............................................................................................................................................14
Data Binding..........................................................................................................................................15
Service ..................................................................................................................................................15
Directive................................................................................................................................................15
Dependency Injection ...........................................................................................................................16
ANGULAR 2 — MODULES..................................................................................................17
ANGULAR 2 — COMPONENTS...........................................................................................21
ANGULAR 2 — TEMPLATES................................................................................................25
Angular 2
iii
ANGULAR 2 — METADATA................................................................................................29
ANGULAR 2 — DATA BINDING...........................................................................................35
ANGULAR 2 — DATA DISPLAY............................................................................................39
ANGULAR 2 — USER INPUT...............................................................................................44
Angular 2 - Binding User Input ..............................................................................................................44
Angular 2 - User Input from Event Object..............................................................................................49
Angular 2 - User Input from Local Template Variable ............................................................................52
Angular 2 - Key Event Filtering ..............................................................................................................55
Angular 2 - On Blur Event ......................................................................................................................59
ANGULAR 2 — FORMS.......................................................................................................63
ANGULAR 2 — SERVICES ...................................................................................................70
ANGULAR 2 — DIRECTIVES................................................................................................77
Angular 2 - Components........................................................................................................................77
Angular 2 - Structural Directives............................................................................................................81
Angular 2 - Attribute Directives.............................................................................................................85
ANGULAR 2 — DEPENDENCY INJECTION ...........................................................................92
Angular 2
4
Angular 2 is an open source JavaScript framework to build web applications in HTML and
JavaScript, and has been conceived as a mobile first approach. The beta version of Angular 2
was released in March 2014.
Why use Angular 2?
 Angular 2 is simpler than Angular 1. The concepts here are easier to understand.
 You can update the large data sets with minimal memory overhead.
 It will speed up the initial load through server side rendering.
Features of Angular 2
 Angular 2 is faster and easier than Angular 1.
 It supports the latest version of browsers and also supports old browsers including
IE9+ and Android 4.1+.
 It is a cross-platform framework.
 Angular 2 is mainly focused on mobile apps.
 Code structure is more simplified than the previous version of Angular.
Advantages of Angular 2
 If an application is heavy, then Angular 2 keeps it fully UI (User Interface) responsive.
 It uses the server side rendering for fast views on mobile.
 It works well with ECMAScript and other languages that compile with JavaScript.
 It uses dependency injection to maintain applications without writing lengthy codes.
 The applications here have a component-based approach.
Disadvantages of Angular 2
 Since Angular 2 is a newly introduced framework, there is less online community
support.
 It takes time to learn if you are new to Angular 2.
ANGULAR 2 — OVERVIEW
Angular 2
5
In this chapter, let us discuss the Angular 2 development environment in detail.
 Angular uses TypeScript, which is a primary language for developing Angular
applications.
 TypeScript is a super set of JavaScript, which is migrated to TypeScript. Here, the code
written in TypeScript makes it less prone to runtime errors.
To set up the development environment, follow these steps:
Step 1: Create a project folder in your local drive by typing the commands in the command
prompt as given below.
mkdir angular2-demo
cd angular2-demo
CreatingConfigurationFiles
The creation of configuration files follows the step mentioned above.
Step 2: You need to create tsconfig.json which is the TypeScript compiler configuration file.
It guides the compiler to generate JavaScript files.
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
ANGULAR 2 — ENVIRONMENT
Angular 2
6
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
Step 3: Create a typings.json file in your project folder angular2-demo as shown below:
typings.json
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160602141332",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"node": "registry:dt/node#6.0.0+20160621231320"
}
}
A large number of libraries of the JavaScript extends JavaScript environment with features
and syntax which is not natively recognized by the TypeScript compiler. The typings.json file
is used to identify TypeScript definition files in your Angular application.
In the above code, there are three typing files as shown below:
 core-js: It brings ES2015/ES6 capabilities to our ES5 browsers.
 jasmine: It is the typing for Jasmine test framework.
 node: It is used for the code that references objects in the nodejs environment.
These typing files are used in the development of larger Angular applications.
Step 4: Add the package.json file to your angular2-demo project folder with the code given
below:
package.json
{
"name": "angular2-demo",
"version": "1.0.0",
"scripts": {
Angular 2
7
"start": "concurrent "npm run tsc:w" "npm run lite" ",
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"typings": "typings",
"postinstall": "typings install"
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.7",
"systemjs": "0.19.22",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.5.15"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.1.0",
"typescript": "^1.7.5",
"typings":"^0.6.8"
}
}
The package.json will contain the packages that our apps require. These packages are
installed and maintained with npm (Node Package Manager). To install npm click here.
Step 5: To install packages, run the npm command in the command prompt as given below.
npm install
Error messages in red may appear while installing npm. These messages have to be ignored.
Angular 2
8
CreatingOurFirstAngularComponent
A component is the fundamental concept of Angular. A component is a class that controls a
view template - a part of a web page where information to the user is displayed and user
feedback is responded to. Components are required to build Angular apps.
Step 6: Create a sub-folder called app/ inside your project folder to place the Angular app
components. You can use the following command to create the folder:
mkdir app
cd app
Step 7: The files which you create need to be saved with the .ts extension. Create a file
called environment_app.component.ts in your app/ folder with the below code:
environment_app.component.ts
import {Component, View} from "angular2/core";
@Component({
selector: 'my-app'
})
@View({
template: '<h2>My First Angular 2 App</h2>'
})
export class AppComponent {
}
[[
 The above code will import the Component and the View package
from angular2/core.
 The @Component is an Angular 2 decorator that allows you to associate metadata
with the component class.
 The my-app can be used as HTML tag and also as a component.
 The @view contains a template that tells Angular how to render a view.
 The export specifies that, this component will be available outside the file.
Angular 2
9
Step 8: Next, create the environment_main.ts file with the following code:
environment_main.ts
import {bootstrap} from "angular2/platform/browser"
import {AppComponent} from "./environment_app.component"
bootstrap(AppComponent);
Angular 2
10
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com

More Related Content

Similar to Angular2 tutorial (20)

PPT on Angular 2 Development Tutorial
PPT on Angular 2 Development TutorialPPT on Angular 2 Development Tutorial
PPT on Angular 2 Development Tutorial
Paddy Lock
 
Angular 2 overview
Angular 2 overviewAngular 2 overview
Angular 2 overview
Jesse Warden
 
Reason to choose Angular JS for your Web Application
Reason to choose Angular JS for your Web ApplicationReason to choose Angular JS for your Web Application
Reason to choose Angular JS for your Web Application
Priyanka Verma
 
Angular 2.0
Angular  2.0Angular  2.0
Angular 2.0
Mallikarjuna G D
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
valuebound
 
Angular 2
Angular 2Angular 2
Angular 2
Nigam Goyal
 
Angular2 getting started by Stephen Lautier
Angular2 getting started by Stephen LautierAngular2 getting started by Stephen Lautier
Angular2 getting started by Stephen Lautier
Andrei Toma
 
Angular 2 - How we got here?
Angular 2 - How we got here?Angular 2 - How we got here?
Angular 2 - How we got here?
Marios Fakiolas
 
Adventures with Angular 2
Adventures with Angular 2Adventures with Angular 2
Adventures with Angular 2
Dragos Ionita
 
Angular Notes.pdf
Angular Notes.pdfAngular Notes.pdf
Angular Notes.pdf
sagarpal60
 
Angular
AngularAngular
Angular
TejinderMakkar
 
An evening with Angular 2
An evening with Angular 2An evening with Angular 2
An evening with Angular 2
Mike Melusky
 
What's new in Angular 2?
What's new in Angular 2?What's new in Angular 2?
What's new in Angular 2?
Alfred Jett Grandeza
 
Neoito — A roadmap to Angular
Neoito — A roadmap to AngularNeoito — A roadmap to Angular
Neoito — A roadmap to Angular
Neoito
 
An afternoon with angular 2
An afternoon with angular 2An afternoon with angular 2
An afternoon with angular 2
Mike Melusky
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?
jbandi
 
Angular, the New Angular JS
Angular, the New Angular JSAngular, the New Angular JS
Angular, the New Angular JS
Kenzan
 
Angular2
Angular2Angular2
Angular2
Oswald Campesato
 
Moving From AngularJS to Angular 2
Moving From AngularJS to  Angular 2 Moving From AngularJS to  Angular 2
Moving From AngularJS to Angular 2
Exilesoft
 
5 Key Benefits of Migration
5 Key Benefits of Migration5 Key Benefits of Migration
5 Key Benefits of Migration
Happiest Minds Technologies
 
PPT on Angular 2 Development Tutorial
PPT on Angular 2 Development TutorialPPT on Angular 2 Development Tutorial
PPT on Angular 2 Development Tutorial
Paddy Lock
 
Angular 2 overview
Angular 2 overviewAngular 2 overview
Angular 2 overview
Jesse Warden
 
Reason to choose Angular JS for your Web Application
Reason to choose Angular JS for your Web ApplicationReason to choose Angular JS for your Web Application
Reason to choose Angular JS for your Web Application
Priyanka Verma
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
valuebound
 
Angular2 getting started by Stephen Lautier
Angular2 getting started by Stephen LautierAngular2 getting started by Stephen Lautier
Angular2 getting started by Stephen Lautier
Andrei Toma
 
Angular 2 - How we got here?
Angular 2 - How we got here?Angular 2 - How we got here?
Angular 2 - How we got here?
Marios Fakiolas
 
Adventures with Angular 2
Adventures with Angular 2Adventures with Angular 2
Adventures with Angular 2
Dragos Ionita
 
Angular Notes.pdf
Angular Notes.pdfAngular Notes.pdf
Angular Notes.pdf
sagarpal60
 
An evening with Angular 2
An evening with Angular 2An evening with Angular 2
An evening with Angular 2
Mike Melusky
 
Neoito — A roadmap to Angular
Neoito — A roadmap to AngularNeoito — A roadmap to Angular
Neoito — A roadmap to Angular
Neoito
 
An afternoon with angular 2
An afternoon with angular 2An afternoon with angular 2
An afternoon with angular 2
Mike Melusky
 
Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?
jbandi
 
Angular, the New Angular JS
Angular, the New Angular JSAngular, the New Angular JS
Angular, the New Angular JS
Kenzan
 
Moving From AngularJS to Angular 2
Moving From AngularJS to  Angular 2 Moving From AngularJS to  Angular 2
Moving From AngularJS to Angular 2
Exilesoft
 

More from HarikaReddy115 (20)

Dbms tutorial
Dbms tutorialDbms tutorial
Dbms tutorial
HarikaReddy115
 
Data structures algorithms_tutorial
Data structures algorithms_tutorialData structures algorithms_tutorial
Data structures algorithms_tutorial
HarikaReddy115
 
Wireless communication tutorial
Wireless communication tutorialWireless communication tutorial
Wireless communication tutorial
HarikaReddy115
 
Cryptography tutorial
Cryptography tutorialCryptography tutorial
Cryptography tutorial
HarikaReddy115
 
Cosmology tutorial
Cosmology tutorialCosmology tutorial
Cosmology tutorial
HarikaReddy115
 
Control systems tutorial
Control systems tutorialControl systems tutorial
Control systems tutorial
HarikaReddy115
 
Computer logical organization_tutorial
Computer logical organization_tutorialComputer logical organization_tutorial
Computer logical organization_tutorial
HarikaReddy115
 
Computer fundamentals tutorial
Computer fundamentals tutorialComputer fundamentals tutorial
Computer fundamentals tutorial
HarikaReddy115
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
HarikaReddy115
 
Communication technologies tutorial
Communication technologies tutorialCommunication technologies tutorial
Communication technologies tutorial
HarikaReddy115
 
Biometrics tutorial
Biometrics tutorialBiometrics tutorial
Biometrics tutorial
HarikaReddy115
 
Behavior driven development_tutorial
Behavior driven development_tutorialBehavior driven development_tutorial
Behavior driven development_tutorial
HarikaReddy115
 
Basics of computers_tutorial
Basics of computers_tutorialBasics of computers_tutorial
Basics of computers_tutorial
HarikaReddy115
 
Basics of computer_science_tutorial
Basics of computer_science_tutorialBasics of computer_science_tutorial
Basics of computer_science_tutorial
HarikaReddy115
 
Basic electronics tutorial
Basic electronics tutorialBasic electronics tutorial
Basic electronics tutorial
HarikaReddy115
 
Auditing tutorial
Auditing tutorialAuditing tutorial
Auditing tutorial
HarikaReddy115
 
Artificial neural network_tutorial
Artificial neural network_tutorialArtificial neural network_tutorial
Artificial neural network_tutorial
HarikaReddy115
 
Artificial intelligence tutorial
Artificial intelligence tutorialArtificial intelligence tutorial
Artificial intelligence tutorial
HarikaReddy115
 
Antenna theory tutorial
Antenna theory tutorialAntenna theory tutorial
Antenna theory tutorial
HarikaReddy115
 
Analog communication tutorial
Analog communication tutorialAnalog communication tutorial
Analog communication tutorial
HarikaReddy115
 
Data structures algorithms_tutorial
Data structures algorithms_tutorialData structures algorithms_tutorial
Data structures algorithms_tutorial
HarikaReddy115
 
Wireless communication tutorial
Wireless communication tutorialWireless communication tutorial
Wireless communication tutorial
HarikaReddy115
 
Control systems tutorial
Control systems tutorialControl systems tutorial
Control systems tutorial
HarikaReddy115
 
Computer logical organization_tutorial
Computer logical organization_tutorialComputer logical organization_tutorial
Computer logical organization_tutorial
HarikaReddy115
 
Computer fundamentals tutorial
Computer fundamentals tutorialComputer fundamentals tutorial
Computer fundamentals tutorial
HarikaReddy115
 
Compiler design tutorial
Compiler design tutorialCompiler design tutorial
Compiler design tutorial
HarikaReddy115
 
Communication technologies tutorial
Communication technologies tutorialCommunication technologies tutorial
Communication technologies tutorial
HarikaReddy115
 
Behavior driven development_tutorial
Behavior driven development_tutorialBehavior driven development_tutorial
Behavior driven development_tutorial
HarikaReddy115
 
Basics of computers_tutorial
Basics of computers_tutorialBasics of computers_tutorial
Basics of computers_tutorial
HarikaReddy115
 
Basics of computer_science_tutorial
Basics of computer_science_tutorialBasics of computer_science_tutorial
Basics of computer_science_tutorial
HarikaReddy115
 
Basic electronics tutorial
Basic electronics tutorialBasic electronics tutorial
Basic electronics tutorial
HarikaReddy115
 
Artificial neural network_tutorial
Artificial neural network_tutorialArtificial neural network_tutorial
Artificial neural network_tutorial
HarikaReddy115
 
Artificial intelligence tutorial
Artificial intelligence tutorialArtificial intelligence tutorial
Artificial intelligence tutorial
HarikaReddy115
 
Antenna theory tutorial
Antenna theory tutorialAntenna theory tutorial
Antenna theory tutorial
HarikaReddy115
 
Analog communication tutorial
Analog communication tutorialAnalog communication tutorial
Analog communication tutorial
HarikaReddy115
 
Ad

Recently uploaded (20)

AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
Mani Sasidharan
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General QuizPragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya - UEM Kolkata Quiz Club
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
How to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time OffHow to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time Off
Celine George
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
AR3201 WORLD ARCHITECTURE AND URBANISM EARLY CIVILISATIONS TO RENAISSANCE QUE...
Mani Sasidharan
 
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
How to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time OffHow to Manage Allocations in Odoo 18 Time Off
How to Manage Allocations in Odoo 18 Time Off
Celine George
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
Ad

Angular2 tutorial

  • 2. Angular 2 i AbouttheTutorial Angular 2 is an open source JavaScript framework to build web applications in HTML and JavaScript, and has been conceived as a mobile first approach. Audience This tutorial is designed for software professionals who want to learn the basics of AngularJS 2 and its programming concepts in simple and easy steps. It describes the components of AngularJS 2 with suitable examples. Prerequisites You should have a basic understanding of JavaScript and any text editor. As we are going to develop web-based applications using Angular 2, it will helpful if you have an understanding of other web technologies such as HTML, CSS, AJAX, AngularJS, etc. Copyright&Disclaimer  Copyright 2016 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected]
  • 3. Angular 2 ii TableofContents About the Tutorial....................................................................................................................................i Audience..................................................................................................................................................i Prerequisites............................................................................................................................................i Copyright & Disclaimer.............................................................................................................................i Table of Contents....................................................................................................................................ii ANGULAR 2 — OVERVIEW...................................................................................................1 ANGULAR 2 — ENVIRONMENT............................................................................................3 Creating Configuration Files ....................................................................................................................3 Creating Our First Angular Component ...................................................................................................6 Compile and Run.....................................................................................................................................8 ANGULAR 2 — HELLO WORLD.............................................................................................9 ANGULAR 2 — ARCHITECTURE..........................................................................................13 Module .................................................................................................................................................14 Component ...........................................................................................................................................14 Template...............................................................................................................................................14 Metadata ..............................................................................................................................................14 Data Binding..........................................................................................................................................15 Service ..................................................................................................................................................15 Directive................................................................................................................................................15 Dependency Injection ...........................................................................................................................16 ANGULAR 2 — MODULES..................................................................................................17 ANGULAR 2 — COMPONENTS...........................................................................................21 ANGULAR 2 — TEMPLATES................................................................................................25
  • 4. Angular 2 iii ANGULAR 2 — METADATA................................................................................................29 ANGULAR 2 — DATA BINDING...........................................................................................35 ANGULAR 2 — DATA DISPLAY............................................................................................39 ANGULAR 2 — USER INPUT...............................................................................................44 Angular 2 - Binding User Input ..............................................................................................................44 Angular 2 - User Input from Event Object..............................................................................................49 Angular 2 - User Input from Local Template Variable ............................................................................52 Angular 2 - Key Event Filtering ..............................................................................................................55 Angular 2 - On Blur Event ......................................................................................................................59 ANGULAR 2 — FORMS.......................................................................................................63 ANGULAR 2 — SERVICES ...................................................................................................70 ANGULAR 2 — DIRECTIVES................................................................................................77 Angular 2 - Components........................................................................................................................77 Angular 2 - Structural Directives............................................................................................................81 Angular 2 - Attribute Directives.............................................................................................................85 ANGULAR 2 — DEPENDENCY INJECTION ...........................................................................92
  • 5. Angular 2 4 Angular 2 is an open source JavaScript framework to build web applications in HTML and JavaScript, and has been conceived as a mobile first approach. The beta version of Angular 2 was released in March 2014. Why use Angular 2?  Angular 2 is simpler than Angular 1. The concepts here are easier to understand.  You can update the large data sets with minimal memory overhead.  It will speed up the initial load through server side rendering. Features of Angular 2  Angular 2 is faster and easier than Angular 1.  It supports the latest version of browsers and also supports old browsers including IE9+ and Android 4.1+.  It is a cross-platform framework.  Angular 2 is mainly focused on mobile apps.  Code structure is more simplified than the previous version of Angular. Advantages of Angular 2  If an application is heavy, then Angular 2 keeps it fully UI (User Interface) responsive.  It uses the server side rendering for fast views on mobile.  It works well with ECMAScript and other languages that compile with JavaScript.  It uses dependency injection to maintain applications without writing lengthy codes.  The applications here have a component-based approach. Disadvantages of Angular 2  Since Angular 2 is a newly introduced framework, there is less online community support.  It takes time to learn if you are new to Angular 2. ANGULAR 2 — OVERVIEW
  • 6. Angular 2 5 In this chapter, let us discuss the Angular 2 development environment in detail.  Angular uses TypeScript, which is a primary language for developing Angular applications.  TypeScript is a super set of JavaScript, which is migrated to TypeScript. Here, the code written in TypeScript makes it less prone to runtime errors. To set up the development environment, follow these steps: Step 1: Create a project folder in your local drive by typing the commands in the command prompt as given below. mkdir angular2-demo cd angular2-demo CreatingConfigurationFiles The creation of configuration files follows the step mentioned above. Step 2: You need to create tsconfig.json which is the TypeScript compiler configuration file. It guides the compiler to generate JavaScript files. { "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, ANGULAR 2 — ENVIRONMENT
  • 7. Angular 2 6 "exclude": [ "node_modules", "typings/main", "typings/main.d.ts" ] } Step 3: Create a typings.json file in your project folder angular2-demo as shown below: typings.json { "globalDependencies": { "core-js": "registry:dt/core-js#0.0.0+20160602141332", "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", "node": "registry:dt/node#6.0.0+20160621231320" } } A large number of libraries of the JavaScript extends JavaScript environment with features and syntax which is not natively recognized by the TypeScript compiler. The typings.json file is used to identify TypeScript definition files in your Angular application. In the above code, there are three typing files as shown below:  core-js: It brings ES2015/ES6 capabilities to our ES5 browsers.  jasmine: It is the typing for Jasmine test framework.  node: It is used for the code that references objects in the nodejs environment. These typing files are used in the development of larger Angular applications. Step 4: Add the package.json file to your angular2-demo project folder with the code given below: package.json { "name": "angular2-demo", "version": "1.0.0", "scripts": {
  • 8. Angular 2 7 "start": "concurrent "npm run tsc:w" "npm run lite" ", "tsc": "tsc", "tsc:w": "tsc -w", "lite": "lite-server", "typings": "typings", "postinstall": "typings install" }, "license": "ISC", "dependencies": { "angular2": "2.0.0-beta.7", "systemjs": "0.19.22", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.2", "zone.js": "0.5.15" }, "devDependencies": { "concurrently": "^2.0.0", "lite-server": "^2.1.0", "typescript": "^1.7.5", "typings":"^0.6.8" } } The package.json will contain the packages that our apps require. These packages are installed and maintained with npm (Node Package Manager). To install npm click here. Step 5: To install packages, run the npm command in the command prompt as given below. npm install Error messages in red may appear while installing npm. These messages have to be ignored.
  • 9. Angular 2 8 CreatingOurFirstAngularComponent A component is the fundamental concept of Angular. A component is a class that controls a view template - a part of a web page where information to the user is displayed and user feedback is responded to. Components are required to build Angular apps. Step 6: Create a sub-folder called app/ inside your project folder to place the Angular app components. You can use the following command to create the folder: mkdir app cd app Step 7: The files which you create need to be saved with the .ts extension. Create a file called environment_app.component.ts in your app/ folder with the below code: environment_app.component.ts import {Component, View} from "angular2/core"; @Component({ selector: 'my-app' }) @View({ template: '<h2>My First Angular 2 App</h2>' }) export class AppComponent { } [[  The above code will import the Component and the View package from angular2/core.  The @Component is an Angular 2 decorator that allows you to associate metadata with the component class.  The my-app can be used as HTML tag and also as a component.  The @view contains a template that tells Angular how to render a view.  The export specifies that, this component will be available outside the file.
  • 10. Angular 2 9 Step 8: Next, create the environment_main.ts file with the following code: environment_main.ts import {bootstrap} from "angular2/platform/browser" import {AppComponent} from "./environment_app.component" bootstrap(AppComponent);
  • 11. Angular 2 10 End of ebook preview If you liked what you saw… Buy it from our store @ https://store.tutorialspoint.com