Skip to content

Commit 5d5c944

Browse files
authored
Update README.md
1 parent 518804b commit 5d5c944

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,152 @@
22

33
<details>
44

5+
<summary><strong>Angular Architectural Questions & Answers</strong></summary>
6+
7+
### What is Angular architecture?
8+
Ans. Angular follows a component-based architecture where the UI is split into components, each with its own template, logic, and styles. These components are organized into a dependency-injection-based system and connected using routing, services, and reactive patterns (RxJS, Reactive Forms, Signals).
9+
10+
### What are standalone components? Why did Angular adopt them?
11+
Ans. Standalone components remove NgModules. They make Angular more lightweight and modular.
12+
**Benefits:**
13+
- Less boilerplate
14+
- Tree-shakeable
15+
- Faster build & better performance
16+
- Simplified folder structure
17+
- Clear and direct imports
18+
19+
### How does Change Detection work in Angular?
20+
Ans. Angular runs change detection using zone.js (or zone-less mode), executing a check cycle to update DOM whenever model changes.
21+
**Modes:**
22+
- Default – checks entire tree
23+
- OnPush – checks only for @Input change, event emission, or Observable async pipe change
24+
- Signal-based reactivity (v16+) uses fine-grained reactivity
25+
26+
### How Many Dependency Injectors Does Angular Have?
27+
Ans. Angular provides two main types of injectors:
28+
```
29+
┌──────────────────────────┐
30+
│ Null Injector │
31+
└────────────┬─────────────┘
32+
33+
┌────────────▼─────────────┐
34+
│ Root Injector │
35+
│ (providedIn: 'root') │
36+
└────────────┬─────────────┘
37+
38+
┌──────────────┴──────────────┐
39+
│ │
40+
┌──────────────▼──────────────┐ ┌──────────▼───────────┐
41+
│ Root Environment Injector │ │ Router Env Injector│
42+
│ (bootstrapApplication()) │ │ (route-level DI) │
43+
└──────────────┬──────────────┘ └──────────┬───────────┘
44+
│ │
45+
│ ┌──────────▼───────────┐
46+
│ │ Lazy Route Injector │
47+
│ │ (Lazy-loaded modules) │
48+
│ └──────────┬───────────┘
49+
│ │
50+
┌─────────────▼─────────────┐ ┌───────────▼────────────┐
51+
│ Component Injectors │ │ Component Injectors │
52+
│ (providers/viewProviders) │ │ for Lazy Loaded Components│
53+
└─────────────┬─────────────┘ └───────────┬────────────┘
54+
│ │
55+
┌─────────▼─────────┐ ┌────────▼─────────┐
56+
│ Directive Injectors│ │ Directive Injectors│
57+
└────────────────────┘ └────────────────────┘
58+
```
59+
60+
**1. Module Injector (a.k.a. Root Injector)**
61+
This is created when the application starts. It holds:
62+
- Services provided in @Injectable({ providedIn: 'root' })
63+
- Providers listed in AppModule or other NgModules
64+
👉 There is exactly one root/module injector per Angular app.
65+
66+
**2. Element Injectors (a.k.a. Node/Component Injectors)**
67+
Angular creates one injector for every component and directive instance if they have providers. Examples that create element injectors:
68+
- providers: [...] in a component
69+
- viewProviders: [...]
70+
- providers on a directive
71+
📌 So the number of element injectors = number of component/directive instances that define providers.
72+
You may have hundreds or thousands of element injectors depending on the DOM.
73+
```
74+
@Component({
75+
selector: 'my-cmp',
76+
providers: [AService],
77+
viewProviders: [BService]
78+
})
79+
80+
┌─────────────────────┐
81+
│ Parent Injector │
82+
└──────────┬───────────┘
83+
84+
┌─────────────────▼─────────────────┐
85+
│ Component Injector │
86+
│ provides: AService │
87+
│ viewProviders: BService │
88+
└───────────────┬───────────────────┘
89+
90+
┌─────────────────────▼─────────────────────┐
91+
│ Directive Injectors inside template │
92+
│ (can access AService but not BService) │
93+
└────────────────────────────────────────────┘
94+
95+
```
96+
97+
**3. Environment Injector**
98+
Introduced with standalone APIs. Created for:
99+
- bootstrapApplication()
100+
- Providers passed via provide*() functions
101+
- Route-level providers (providers: [...] in route config)
102+
👉 There may be multiple environment injectors (e.g., root environment + route-based environments).
103+
```
104+
bootstrapApplication(AppComponent, {
105+
providers: [
106+
provideZoneChangeDetection(),
107+
provideHttpClient(),
108+
...
109+
]
110+
})
111+
112+
┌─────────────────────────────────────────┐
113+
│ Root Environment Injector │
114+
│ (from bootstrapApplication()) │
115+
└───────────────┬─────────────────────────┘
116+
117+
┌─────────────▼──────────────┐
118+
│ Root Injector │
119+
│ (providedIn: 'root') │
120+
└─────────────┬──────────────┘
121+
122+
┌────────────▼────────────┐
123+
│ AppComponent Injector │
124+
│ (if component has DI) │
125+
└────────────┬────────────┘
126+
127+
┌──────────────────▼──────────────────┐
128+
│ Child Components → Their Injectors │
129+
│ Directives → Their Injectors │
130+
└─────────────────────────────────────┘
131+
```
132+
133+
**4. Router Injectors**
134+
Angular router creates:
135+
- A Router Environment Injector for route-level providers
136+
- A Component Route Injector for lazy-loaded routes
137+
- These injectors form child nodes in the injector hierarchy.
138+
Lazy-loaded routes introduce a lazy route injector, isolating providers.
139+
```
140+
{
141+
path: 'products',
142+
loadComponent: () => import('./products.component'),
143+
providers: [ProductsApi]
144+
}
145+
```
146+
147+
</details>
148+
149+
<details>
150+
5151
<summary><strong>Angular Dependency Injection</strong></summary>
6152

7153
### Why use Inject instead of DI with the Constructor ?

0 commit comments

Comments
 (0)