-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasics-Angular-Examples
151 lines (125 loc) · 3.26 KB
/
Basics-Angular-Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Angular Examples Repository
// Example 1. Creating a New Angular Project
```
ng new my-angular-app
```
// Example 2. Simple Angular Component
```
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
<button (click)="changeTitle()">Change Title</button>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Hello, Angular 19!';
changeTitle() {
this.title = 'Title Changed!';
}
}
```
// Example 3. Standalone Component
```
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `<h1>Hello Angular!</h1>`
})
export class AppComponent {}
```
// Example 4. Angular Service with Dependency Injection
```
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataService {
getData() {
return [1, 2, 3];
}
}
```
// Example 5. Using the Service in a Component
```
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-numbers',
template: `<div *ngFor="let num of numbers">{{ num }}</div>`
})
export class NumbersComponent implements OnInit {
numbers: number[] = [];
constructor(private dataService: DataService) {}
ngOnInit() {
this.numbers = this.dataService.getData();
}
}
```
// Example 6. HTTP Client Example
```
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataService {
constructor(private http: HttpClient) {}
fetchData() {
return this.http.get('https://api.example.com/data');
}
}
```
// Example 7. HTTP Interceptor
```
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const cloned = req.clone({
headers: req.headers.set('Authorization', 'Bearer token')
});
return next.handle(cloned);
}
}
```
// Example 8. Lazy Loading Feature Module
```
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FeatureComponent } from './feature.component';
const routes: Routes = [{ path: '', component: FeatureComponent }];
@NgModule({
imports: [RouterModule.forChild(routes)],
declarations: [FeatureComponent]
})
export class FeatureModule {}
```
// Example 9. Change Detection with OnPush Strategy
```
import { Component, ChangeDetectionStrategy, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `<p>{{ data }}</p>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChildComponent {
@Input() data!: string;
}
```
// Example 10. Angular Animations - Fade In Effect
```
import { trigger, state, style, transition, animate } from '@angular/animations';
import { Component } from '@angular/core';
@Component({
selector: 'app-box',
template: `<div [@fadeIn]>Hello</div>`,
animations: [
trigger('fadeIn', [
state('void', style({ opacity: 0 })),
transition(':enter', [animate('500ms ease-in', style({ opacity: 1 }))])
])
]
})
export class BoxComponent {}
```