U3 (Angular)
U3 (Angular)
1
Angular Version 10+
2
Syllabus
Front-End Frameworks: What is web framework? Why Web Framework? Web Framework Types.
MVC: What is MVC, MVC Architecture, MVC in Practical, MVC in Web Frameworks.
Angular Version 10+: Angular CLI, Angular Architecture, Angular Project Structure, Angular Lifecycle,
Angular Modules, Angular Components, Angular Data Binding, Directives and Pipes, Angular Services
and Dependency Injections (DI), Angular Routers, Angular Forms.
3
What is Angular?
Angular is a typescript based free and open source web application framework developed by Google.
Angular 10+ is a JavaScript framework which is used to create single page application.
The Angular applications are created with the help of HTML and TypeScript.
Angular Vs AngularJS
4
Angular CLI
Used to perform various operations related to an Angular project. It lets developers create, run,
test and build Angular projects using simple commands. It also lets developers create
Angular CLI is available as a NodeJS module and therefore, to use it, we need to download and
5
Installing Angular CLI
Step 1: Check your machine for node.js and npm
node –v
npm –v
Step 2: Install Angular CLI
npm install -g @angular/cli
Step 3: Check version of Angular
ng version
Step 4: Create workspace folder
ng new Proj_name
Would you like to add Angular routing? Y
Which stylesheet format would you like to use? CSS
Step 5: Change directory to the proj_name folder and issue command
ng serve -o
6
Angular Project Structure
2. src folder: this is the place where we need to put all our application source code
3. app folder: When we want to create any component, service or module, we need to create it
within this app folder.
4. assets folder: you can store static assets like images, icons etc.
7
Angular Project Structure
8. main.ts file
8
Angular Architecture
9
Angular Modules
Angular modules combines components, directives and pipes.
Every angular app has a root module which is called as AppModule. It provides bootstrap mechanism that
If we want to use another custom angular module, then we need to register that module inside the
app.module.ts file.
Every angular application has at least one ngModule class named AppModule and resides in a file
app.module.ts
The @ngModule() is a function that takes a single metadata object, whose properties describe the module.
10
Angular Component
The component in the application defines a class which contains application logic and data.
The application logic is written in TypeScript and view of the page is in HTML.
Every angular app has at least one component known as root component.
Each component consist of:
- HTML declares what displays on the web page
- A TypeScript class that defines behavior
- A CSS selector that defines how the component is used
Command to create custom component
ng generate component component_name
On issuing this command the folder gets created inside the src-> app folder.
It will automatically create .css, .ts, .css and update app.module.ts
11
Angular Data Binding
Data binding defines the communication between components and its view.
Property binding: one way binding in which we can set the properties of the element to the user interface
page.
Example:-
Go to src->app folder and open the file app.component.ts and add image variable public image =
“path of image” in AddComponent class
The event is triggered on the view. The view page sends data to the component.
Example:
Open the app.component.html page and add
<button (click) = “display()”> submit </button>
Open the app.component.ts page and add
display()
{
alert(“Submitted”);
}
in AddComponent class
13
Directives and Pipes
Directives is a technique in Angular that adds additional behavior to the elements in the Angular
applications.
There are three types of directives-
There are three most popularly used structural directives- 1. ngIf 2. ngFor 3. ngSwitch
ng g directive directive_name
14
Directives and Pipes
ngIf Directive:- modify structure of DOM depending on a condition.
Syntax:-
<div *ngIf = “condition”> Content to display when condition is true </div>
Example:
1. Open app.component.html and write
<div *ngIf = “courses.length > 0” >
List of courses
</div>
2. Open app.component.ts and write
courses = [1, 2];
in AddComponent class
15
ngFor Directive: shows the template/repeats itself for each item in the list.
Syntax:
<li *ngFor = “let item of items”> … </li>
Example:
1. Open app.component.ts file and write
students = [
{roll:2,name:’Abhi’},
{roll:4,name:’Shree’}
];
in AddComponent class
Example:
count = 1;
in AddComponent class
</div>
17
Pipes
Pipes are used to transform the data.
Built-in pipes:
Example: