angularr
angularr
**
Angular is a popular open-source web application framework developed and maintained by Google.
It is used to build single-page applications (SPAs) using HTML, CSS, and TypeScript.
2. TypeScript support – Angular uses TypeScript, which is a superset of JavaScript that adds
strong typing.
3. Two-way data binding – Automatically synchronizes data between the model and the view.
7. Angular CLI – A command-line tool to create, build, test, and deploy Angular apps easily.
Angular 4 2017 Skipped Angular 3, improved performance, *ngIf & *ngFor enhancements.
Angular 9 2020 Ivy rendering engine became the default (faster and smaller apps).
Installing angular:
ng new <project-name>
1. Modules
Every app has at least one root module (usually called AppModule).
Modules help organize the app and group related features together.
2. Components
Example: Home page, Course list, Course detail – each can be a separate component.
o HTML (template)
o CSS (style)
o TypeScript (logic)
3. Templates
A template is the HTML that defines how a component looks on the screen.
It contains normal HTML plus some Angular code like {{title}} or *ngIf.
4. Decorators
A decorator is a special function (like @Component, @NgModule) that gives extra info to
Angular about how to use a class.
It helps Angular know what that class does (like whether it’s a component, module, service,
etc.).
5. Metadata
Angular uses it (through decorators) to understand how to process and use the class.
6. Directives
Directives are special instructions in templates that change how things appear or behave.
7. Services
A service is a class that has some useful code or logic (like fetching data).
9. Data Binding
Data Binding is a way to connect your component's data with the HTML view.
It makes the app dynamic—when data changes in the component, it automatically updates
the screen and vice versa.
Component:
1. Template (HTML)
o This is the UI part of the component (what you see on the screen).
o Written in HTML and may include Angular features like {{ }} for displaying data or
*ngIf, *ngFor, etc.
o Example: If you're building a to-do list, the logic for adding or removing items will be
here.
3. Style (CSS)
o This part handles the look and feel of the component – colors, fonts, layout, etc.
Structure of a Component:
// app.component.ts
@Component({
})
//app.component.html
<h1>{{ title }}</h1> <!-- displays data from the component class -->
//app.component.css
h1 {
color: darkblue;
Key Points:
The selector is the custom HTML tag used to insert this component into another template
(like <app-root></app-root>).
Components can use other components inside them (like a parent-child relationship).
MODULE:
A Module in Angular is a container that groups related parts of the app together — such as
components, directives, pipes, and services.
It helps keep your app organized and maintainable, especially when your app grows big.
// app.module.ts
})
1. declarations:
2. imports:
3. providers:
4. bootstrap:
o The root component that Angular should start the app with (usually AppComponent).
Allows lazy loading: load parts of the app only when needed.
Helps with code reuse: create feature modules and import them where required.
Type Description
Directives:
Directive is a special instruction that you can add to HTML to change the behavior or appearance of
elements.
Types of Directives
Eg:
@Component({
selector: 'app-header',
templateUrl: './header.component.html'
})
2. Structural Directives
Eg:
<ul>
</ul>
3. Attribute Directives
Common ones:
Data Binding:
Data Binding in Angular is a technique to connect your component (TypeScript logic) and your
template (HTML view) so they can communicate and stay in sync.
It helps you:
Angular replaces {{ userName }} with the value of the userName property from the component at
runtime.
<img [src]="user.imageUrl">
<input [value]="user.name">
3. Class Binding
To listen to user events (clicks, inputs, etc.) and call functions in your component.
<button (click)="onClick()">Click Me</button>
<input (input)="onInputChange($event)">
onClick() {
onInputChange(event: any) {
Common events:
To bind input values both ways — from component to view AND from view to component.
<input [(ngModel)]="userName">
Angular Services
Angular Services are reusable pieces of code or logic that perform specific tasks. They are designed
to share data, logic, or functionality across multiple components in an Angular application.
Share Data: Services can hold data that multiple components can access or update.
Separation of Concerns: Components handle view logic; services handle business logic or
data logic.
@Injectable({
})
getMessage() {
return this.message;
setMessage(newMsg: string) {
this.message = newMsg;
}
@Component({
selector: 'app-my',
})
ngOnInit() {
this.msg = this.dataService.getMessage();
**
In frontend:
STEPS:
@NgModule({
imports: [
HttpClientModule
})
//api.service.ts
@Injectable({
providedIn: 'root'
})
getGreeting() {
return this.http.get<any>('http://localhost:3000/api/greeting');
}
3.Use it in a component:
@Component({
selector: 'app-home',
})
ngOnInit() {
this.apiService.getGreeting().subscribe(data => {
this.message = data.message;
});
Dependency Injection is a design pattern where Angular automatically provides objects (like services)
to components or other services.
Example of DI
constructor(private apiService: ApiService) {}
Here, Angular injects an instance of ApiService when the component is created. You don't need to
use new ApiService() — Angular handles it.
**
ROUTING:
Routing in Angular means navigating between different views or pages of your application without
reloading the whole page.
Instead of loading new HTML files, Angular just displays the component associated with a particular
route — this is called a Single Page Application (SPA) behavior.
Step-by-Step Example:
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
<nav>
<a routerLink="">Home</a>
<a routerLink="about">About</a>
</nav>
<router-outlet></router-outlet>
Create Components
//MODULE
imports: [ HttpClientModule ]
})
//SERVICE
getData() {
return this.http.get<any>('https://api.example.com/data');
Angular’s HttpClient returns Observables. Observables let you subscribe to data that might arrive
later (like a response from an API).
@Component({
selector: 'app-home',
})
export class HomeComponent implements OnInit {
data: any;
ngOnInit() {
this.api.getData().subscribe(response => {
this.data = response;
});
The .subscribe() method is what actually executes the HTTP request and allows you to use the
result.
Pipes:
Example:
FORMS:
Angular Forms are used to collect and manage user input in your application, like filling out a login
or registration form.
1. Template-Driven Forms:
Easy to use
Logic mostly in the HTML
Ideal for simple forms
Example:
<button type="submit">Submit</button>
</form>
submitForm(form: NgForm) {
console.log(form.value);
2.Reactive Forms
//Module
//Component
import { FormGroup, FormControl } from '@angular/forms';
});
submit() {
console.log(this.form.value);
//html
<input formControlName="username">
<input formControlName="email">
<button type="submit">Submit</button>
</form>
**