Week 3 Angular Angular Architecture CLI
Week 3 Angular Angular Architecture CLI
Development
(CSE-214)
week-3 : Angular Application
Architecture, MVC, CLI
1
Angular Features
2
Angular Architecture
3
1. View Templates (User Interface for
Components)
4
2. Components (The Main Player in
Angular)
component consists of:
view (HTML template) – what the user sees,
class (TypeScript) – contains logic and event handling,
stylesheet (CSS) – styles the component.
5
3. Services (Helper Classes for Business
Logic)
Services fetch data, perform calculations, and handle business logic.
6
3. Services (Helper Classes for Business
Logic)
DataService is injected into the component.
*ngFor loops through the frameworks list and displays the items.
7
4. Directives (Adding Custom Behavior to
HTML)
directive modifies the behavior of an HTML element.
*ngFor for Looping Through Data and displays each item as a list element
8
5. Modules (Organizing Angular Apps)
A module is a collection of related components, directives, and services.
declarations → Lists
components (AppComponent,
HelloComponent).
9
Key Terms
10
Application UI Composition
11
Application Interaction
12
Angular Project
13
Creating an Angular Project
14
Angular CLI
15
Creating New Project with Angular CLI
16
Running the Angular App
17
Changing the Server Port
18
Model-View-Controller (MVC) Design Pattern
19
Model-View-Controller (MVC) Design
Pattern
20
Model-View-Controller (MVC) Design
Pattern
The View displays the data from the Model to the user and sends user
inputs to the Controller. It is passive and does not directly interact with
the Model. Instead, it receives data from the Model and sends user
inputs to the Controller for processing.
21
Model-View-Controller (MVC) Design
Pattern
22
Angular application architecture (1)
23
Angular application architecture (2)
•Angular.json file will contain all the configurations of the
app.
24
package.json file
25
package.json file - Scripts Section
26
package.json file - Dependencies Section
This section lists the core Angular libraries required to run the
application.
27
package.json file - DevDependencies
Section
These packages are only needed during development (not in
production).
28
package.json file - How to Use These
Scripts?
Run the app locally
29
dist/ Folder in Angular
It contains the compiled and optimized version of your Angular application,
ready for deployment.
After running ng build, the dist/ folder contains everything needed to run the
app.
You can deploy this folder (the contents of dist/) to a web server like Apache, Nginx
30
AOT (Ahead-of-Time) vs. JIT (Just-in-
Time) Compilation in Angular
Angular provides two types of compilation:
32
main.ts file
33
Angular application architecture (4)
•AppModule contains declarations of all the components,
this will bootstrap AppComponent (defined in
app.component.ts file)
34
app.module.ts file
app.module.ts is the root module file in an Angular application. It is responsible for:
35
app.component.ts file
app.component.ts is the main component file in an Angular application. It defines:
36
app.component.ts file
37
Angular application architecture (5)
•Each component is declared with three properties:
•Selector - used for accessing the component
•Template/TemplateURL - contains HTML of the
component
•StylesURL - contains component-specific stylesheets
38
Angular application architecture (6)
•After this, Angular calls the index.html file. This file consequently
calls the root component that is app-root. The root component is
defined in app.component.ts
39
index.html file
index.html is the main HTML file where the Angular app runs.
It loads all JavaScript and CSS files needed for the app
40
Angular CLI
Angular CLI stands for Angular Command Line Interface, and it helps developers
to create projects easily and quickly and automate the development workflow.
41
Angular CLI
42
Angular CLI
1. npm : This is the Node Package Manager, which is used to
install and manage packages for Node.js applications.
43
Creating a New Angular Project
cd your-project-name
44
Angular-Files and Folder Structure
45
Angular-Files and Folder Structure
1. /node_modules/ : All 3rd party libraries are installed to this folder using npm
install
2. /src/ : contains the source code of the application. Most work will be done
here
3. /src/app/: This is where your application’s components, services, modules,
and other related files are stored. It’s the heart of your application.
4. index.html : The main HTML file that serves as the entry point for your
application.
5. main.ts : the main starting file from where the AppModule is bootstrapped
6. styles.css : the global stylesheet file for the project
7. tsconfig.*.json : the configuration files for TypeScript
8. angular.json : contains the configurations for CLI. This configuration file
defines various settings for your Angular project, including build options, asset
paths, and other project-specific configurations.
9. package.json : contains the basic information of the project (name,
description and dependencies)
46
Angular-Files and Folder Structure
47
angular.json File Structure
1. Projects : This section contains configurations for one or more projects within your
Angular workspace. Each project represents an application, library, or other
related code within your workspace.
2. architect: This subsection defines the various build, test, and serve tasks you can
run on your project.
3. build: Configures options for building your project for production or development.
You can specify the output paths, assets, styles, scripts, and more.
4. test: Configures testing settings using testing frameworks
5. serve: Configures the development server settings for serving your application
locally during development.
6. sourceRoot: Specifies the root directory where your application’s source code
resides.
48
Bootstrapping the AppModule (main.ts)
• Acts as the starting point (entry point) for the application lifecycle.
• This file is the bootstrap file of the Angular application. Angular uses main.ts to
load the Angular module and start the app.
• When the app starts, the browser loads index.html. It contains the root
<app-root></app-root> element, AppComponent is rendered in the index.html file's
<app-root></app-root> element
This file defines the root NgModule (Angular Module) of the application. It
tells Angular which components, directives, and services are part of the
app.
51
AppComponent (app.component.ts)
53
Execution Flow
index.html is loaded first when the browser requests the
app. It includes a reference to the Angular application and
the root component (<app-root></app-root>).
54
Relationships
55
Running the Development Server
1.Use the following command to start the development server and
run your Angular application locally
ng serve
3. Additional Options
To specify a different port for the development server, you can use
the -- port option.
ng serve -- port 3000
56
Running the Development Server
ng serve
57
Generating Angular Artifacts
58
Generating Angular Artifacts
If you want to add your component, directive or pipe to another
module (other than the main application module, app.module.ts),
you can simply prefix the name of the component with the module
name and a slash :
ng g component my-module/my-component
59
Component in Angular
60
Component in Angular
• A component consists of a template(HTML view of UI), styles(CSS
appearance/design) and a typescript class which contains
business logic.
• To indicate a class as component @Component decorator is used.
• The @Component decorator provides metadata to the
component.
• The component metadata properties consist of selectors,
directives, providers, styleUrls and templateUrls.
61
Component in Angular
•Templates are created using HTML and it binds the component properties and
methods thus helping us to render data dynamically.
64
AppComponent
When you create a new Angular project using the Angular cli command
(ng new project-name), by default it will create a component called
AppComponent in src/app.
65
AppComponent
This AppComponent is the root component, and it holds the
template for the entire application. This root component can nest
several components as its child component. An angular project must
have at least one component.
66
Component in Angular
@Component
This @Component decorator is used to make a class an angular
component and provide additional metadata that determines how
the component should be processed. In the below example, the
component decorator has three properties.
selector: The selector property specifies the custom HTML element selector that represents
the root component.
templateUrl: The templateUrl property specifies the path to the HTML template file that
defines the component’s view.
styleUrls: The styleUrls property is an array that lists the paths to external style files (CSS or
SCSS) for styling the component.
67
Component in Angular
selector: The selector
property specifies the custom
HTML element selector that
represents the root
component.
69
Child Component in Angular
Angular applications typically have a hierarchical structure where components
are organized in a tree-like fashion. At the root, you have the main
component, and it can have child components
For example, to create a webpage like the template shown in the figure,
define the components separately for the nav bar, header, footer, and main
content area. These components can be reused wherever we want.
70
Main.ts file
71
app.module.ts
The app.module.ts is the root module of the Angular application. It's where you
define the structure, components, services, and other features of your
application.
72
Generating Component
73
Generating Component
74
Angular Modules
You can also import and export functionalities from one module to the other for
efficient and clean programming. 75
Angular Modules
When you launch the application, this is the module that gets
bootstrapped.
76
Angular Architecture
77
Angular Modules
• Angular Applications are modular and
Angular has its own modularity system
called NgModule
78
Angular Modules
declarations: declaration property contains a list of the
component which you define for this module.
79
Angular Modules
Launch an application by bootstrapping its root module. During
development you're likely to bootstrap the AppModule in a main.ts
80
Component
The component is the basic building block of User Interface(UI)
Each component is mapped to the template.
Angular Application is a tree of Angular Component
Angular creates, updates and destroys components as the user moves through
the application.
81
Component
selector: the name given in this property is used
on HTML page as a tag to load that component
the screen. For example, to load app-root on
screen you need to use <app-root> on HTML
page.
82
Component Example
83
Template Example
Each component is mapped to one template.
template is a form of HTML that tells Angular how to render the component on
the screen.
Although this template uses typical HTML elements like <h2> and <p>, it also has
some differences. Code like *ngFor, {{hero.name}}, (click), [hero], and <app-
hero-detail>
In the last line of the template, the <app-hero-detail> tag is a custom element
that represents a new component, HeroDetailComponent
84
Template
85
Data Binding
Data Binding in Angular is a mechanism that connects the
component (TypeScript) and the view (HTML), ensuring
seamless synchronization between them.
86
Data Binding
Angular supports the data binding for coordinating parts of a template with the
parts of a component.
Angular supports following types of data binding :
1.Interpolation
2.Property Binding
3.Event Binding
4.Two-way Data Binding
87
Data Binding
88
Data Binding
89
Data Binding
2. The [hero] property binding passes the value of selectedHero from the
parent HeroListComponent to the hero property of the child
HeroDetailComponent.
3. The (click) event binding calls the component's selectHero method when
the user clicks a hero's name. 90
Two-way Data Binding
4. Two-way data binding combines
property and event binding in a single
notation, using the ngModel directive.
91
Metadata
All the parts of angular like component, directive, module or service, all are
basic typescript classes
but the question is How angular know type of class?
92
Directives
93
Structural Directives
*ngFor tells Angular to stamp out one <li> per hero in the heroes list.
94
Attribute Directives
95
Service
Services are used for reusable data services to share between components
throughout an application.
This can be also used as data sharing class, to share data between components
throughout an application as well as writing business logic.
Examples include:
• logging service
• data service
• application configuration
96
Service
97
Dependency Injection
Dependency injection is a way to supply a new instance of a class with the fully-
formed dependencies it requires. Most dependencies are services. Angular uses
dependency injection to provide new components with the services they need.
98
Dependency Injection
Injector maintains the list of Services which you are going to use in the
application. Whenever any component requires the service, injector will give the
instance to that component.
99
Dependency Injection
100
Angular Loading Application
101
Loading Angular App
102
Loading Angular App
103
Loading Angular App
104
Loading Angular App
105
Loading Angular App
106