Angular - Part 2
Angular - Part 2
Angular - Part 2
Web
Development
Part 2 SWEN-261
Introduction to Software
Engineering
Department of Software Engineering
Rochester Institute of Technology
Summary
• Angular is a component-based framework that is used for developing
single page applications employing TypeScript and HTML template
language
• Typescript is a language that compiles to JavaScript. It is strongly typed, object
oriented and compiled language
• HTML (Hypertext Markup Language) is the code that is used to structure a web
page and its content
• CSS (Cascading Style Sheets) is the language we use to style a Web page
• Last class we covered Modules and Components
• Modules are objects that help you to organize dependencies into discrete units
• Components are new elements that will compose the majority of your application’s
structure and logic
Modules vs Components
Module Component
A module is a collection of components, services, A component in Angular is a building block of the
directives, pipes and so on Application with an associated template
Denoted by @NgModule Denoted by @Component
An Angular application will contain many modules, Each component can use other components, which are
each dedicated to a single purpose declared in the same module. To use components
declared in other modules, they need to be exported
from this module and the module needs to be
imported.
Angular – What’s next
• Data binding
• Services
• Routing
• Observables
Angular – Data Binding
• Data binding automatically keeps your page up-to-date based on your
application's state. You use data binding to specify things such as the
source of an image, the state of a button, or data for a particular user
• There are four types of data binding available in Angular:
• Event binding - This data binding type is when information flows from the view to
the component when an event is triggered
• Interpolation - Text representing variables in components are placed in between
double curly braces in the template
• Two-way data binding - Two-way binding is a mechanism where data flows both
ways from the component to the view and back
• Property binding - Property binding is a one-way mechanism that lets you set the
property of a view element
Data Binding – Event Binding
• To bind to an event, you use the Angular event binding syntax
• This syntax consists of a target event name within parentheses to the
left of an equal sign, and a quoted template statement to the right
• In the following example, the target event name is click and the
template statement is onSave():
Target Template
event name statement
Target property
Angular – Data Binding Example
• Using Data binding, we can pass data between the component and template
greet.component.ts
greet.component.html
Event binding
Property binding
Interpolation
ng g service log
Angular Example – Service Details
• Add a new log() method to log messages to the console
log.service.ts greet.component.ts
• import - Register the top-level routes and return the routing module that should
be imported by the root module of the application
• export - exports RouterModule so it will be available throughout the application
Routing
• Our new start page links to other pages
app.component.html
Observable
(publisher) [data] [data] [data]
Observer
(consumer)
subscribes
Observers
• The Observer has three handles to use the data that it receives:
• next - Required. A handler for each delivered value that’s called zero or more times
after execution starts
• error - Optional. A handler for an error notification. An error halts execution of the
observable instance
• complete - Optional. A handler for the execution-complete notification. Delayed
values can continue to be delivered to the next handler after execution is complete.
Observerable Observer
(publisher) (consumer)
subscribe next
listen
error
complete
Observerables – Simple Example
• In this example, we create a simple Observable that publishes a list of
items that are subscribed to by an Observer
app.component.ts
Observable object