Skip to content

Commit ea1751d

Browse files
committed
Implement new components and services in lessons, enhancing state management with signals and improving change detection strategies; update templates and examples for better clarity and functionality.
1 parent c762535 commit ea1751d

10 files changed

Lines changed: 1007 additions & 77 deletions

modules/module-2/lessons/exercises/lesson-2-1-exercise-2-di-hierarquica.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ Crie uma estrutura com:
6666
**global.service.ts**
6767
import { Injectable } from '@angular/core';
6868

69+
@Injectable({
70+
providedIn: 'root'
71+
})
72+
export class GlobalService {
73+
private instanceId = Math.random().toString(36).substr(2, 9);
74+
75+
constructor() {
76+
console.log(`GlobalService criado: ${this.instanceId}`);
77+
}
78+
79+
getInstanceId(): string {
80+
return this.instanceId;
81+
}
82+
83+
getServiceName(): string {
84+
return 'GlobalService (root)';
85+
}
86+
}
87+
import { Injectable } from '@angular/core';
88+
6989
@Injectable({
7090
providedIn: 'root'
7191
})
@@ -110,6 +130,24 @@ export class GlobalService {
110130
**component.service.ts**
111131
import { Injectable } from '@angular/core';
112132

133+
@Injectable()
134+
export class ComponentService {
135+
private instanceId = Math.random().toString(36).substr(2, 9);
136+
137+
constructor() {
138+
console.log(`ComponentService criado: ${this.instanceId}`);
139+
}
140+
141+
getInstanceId(): string {
142+
return this.instanceId;
143+
}
144+
145+
getServiceName(): string {
146+
return 'ComponentService (component level)';
147+
}
148+
}
149+
import { Injectable } from '@angular/core';
150+
113151
@Injectable()
114152
export class ComponentService {
115153
private instanceId = Math.random().toString(36).substr(2, 9);
@@ -161,8 +199,13 @@ import { ChildComponent } from './child.component';
161199
template: `
162200
<div class="parent">
163201
<h2>Componente Pai</h2>
202+
{% raw %}
203+
164204
<p>GlobalService ID: {{ globalService.getInstanceId() }}</p>
205+
{% endraw %}
206+
165207
<p>ComponentService ID: {{ componentService.getInstanceId() }}</p>
208+
166209
<app-child></app-child>
167210
</div>
168211
`
@@ -177,6 +220,34 @@ export class ParentComponent {
177220
}
178221
}
179222
{% raw %}
223+
import { Component } from '@angular/core';
224+
import { GlobalService } from './global.service';
225+
import { ComponentService } from './component.service';
226+
import { ChildComponent } from './child.component';
227+
228+
@Component({
229+
selector: 'app-parent',
230+
standalone: true,
231+
imports: [ChildComponent],
232+
providers: [ComponentService],
233+
template: `
234+
<div class="parent">
235+
<h2>Componente Pai</h2>
236+
<p>GlobalService ID: {{ globalService.getInstanceId() }}</p>
237+
<p>ComponentService ID: {{ componentService.getInstanceId() }}</p>
238+
<app-child></app-child>
239+
</div>
240+
`
241+
})
242+
export class ParentComponent {
243+
constructor(
244+
public globalService: GlobalService,
245+
public componentService: ComponentService
246+
) {
247+
console.log('ParentComponent - GlobalService:', this.globalService.getInstanceId());
248+
console.log('ParentComponent - ComponentService:', this.componentService.getInstanceId());
249+
}
250+
}
180251
```typescript
181252
import { Component } from '@angular/core';
182253
import { GlobalService } from './global.service';
@@ -220,8 +291,13 @@ import { ComponentService } from './component.service';
220291
template: `
221292
<div class="child">
222293
<h3>Componente Filho</h3>
294+
{% raw %}
295+
223296
<p>GlobalService ID: {{ globalService.getInstanceId() }}</p>
297+
{% endraw %}
298+
224299
<p>ComponentService ID: {{ componentService.getInstanceId() }}</p>
300+
225301
<p class="note">
226302
GlobalService: Mesma instância (root)<br>
227303
ComponentService: Mesma instância do pai (herdado)
@@ -239,6 +315,34 @@ export class ChildComponent {
239315
}
240316
}
241317
{% raw %}
318+
import { Component } from '@angular/core';
319+
import { GlobalService } from './global.service';
320+
import { ComponentService } from './component.service';
321+
322+
@Component({
323+
selector: 'app-child',
324+
standalone: true,
325+
template: `
326+
<div class="child">
327+
<h3>Componente Filho</h3>
328+
<p>GlobalService ID: {{ globalService.getInstanceId() }}</p>
329+
<p>ComponentService ID: {{ componentService.getInstanceId() }}</p>
330+
<p class="note">
331+
GlobalService: Mesma instância (root)<br>
332+
ComponentService: Mesma instância do pai (herdado)
333+
</p>
334+
</div>
335+
`
336+
})
337+
export class ChildComponent {
338+
constructor(
339+
public globalService: GlobalService,
340+
public componentService: ComponentService
341+
) {
342+
console.log('ChildComponent - GlobalService:', this.globalService.getInstanceId());
343+
console.log('ChildComponent - ComponentService:', this.componentService.getInstanceId());
344+
}
345+
}
242346
```typescript
243347
import { Component } from '@angular/core';
244348
import { GlobalService } from './global.service';

modules/module-2/lessons/exercises/lesson-2-1-exercise-3-providers-escopos.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ Demonstre diferenças criando múltiplos componentes e módulos lazy-loaded.
6565
**root.service.ts**
6666
import { Injectable } from '@angular/core';
6767

68+
@Injectable({
69+
providedIn: 'root'
70+
})
71+
export class RootService {
72+
private instanceId = Math.random().toString(36).substr(2, 9);
73+
74+
constructor() {
75+
console.log(`[ROOT] RootService criado: ${this.instanceId}`);
76+
}
77+
78+
getInstanceId(): string {
79+
return this.instanceId;
80+
}
81+
82+
getScope(): string {
83+
return 'root - Singleton global';
84+
}
85+
}
86+
import { Injectable } from '@angular/core';
87+
6888
@Injectable({
6989
providedIn: 'root'
7090
})
@@ -109,6 +129,26 @@ export class RootService {
109129
**any.service.ts**
110130
import { Injectable } from '@angular/core';
111131

132+
@Injectable({
133+
providedIn: 'any'
134+
})
135+
export class AnyService {
136+
private instanceId = Math.random().toString(36).substr(2, 9);
137+
138+
constructor() {
139+
console.log(`[ANY] AnyService criado: ${this.instanceId}`);
140+
}
141+
142+
getInstanceId(): string {
143+
return this.instanceId;
144+
}
145+
146+
getScope(): string {
147+
return 'any - Nova instância por módulo lazy';
148+
}
149+
}
150+
import { Injectable } from '@angular/core';
151+
112152
@Injectable({
113153
providedIn: 'any'
114154
})
@@ -153,6 +193,24 @@ export class AnyService {
153193
**component-scoped.service.ts**
154194
import { Injectable } from '@angular/core';
155195

196+
@Injectable()
197+
export class ComponentScopedService {
198+
private instanceId = Math.random().toString(36).substr(2, 9);
199+
200+
constructor() {
201+
console.log(`[COMPONENT] ComponentScopedService criado: ${this.instanceId}`);
202+
}
203+
204+
getInstanceId(): string {
205+
return this.instanceId;
206+
}
207+
208+
getScope(): string {
209+
return 'component - Nova instância por componente';
210+
}
211+
}
212+
import { Injectable } from '@angular/core';
213+
156214
@Injectable()
157215
export class ComponentScopedService {
158216
private instanceId = Math.random().toString(36).substr(2, 9);
@@ -206,20 +264,35 @@ import { ComponentScopedService } from './component-scoped.service';
206264

207265
<div class="service-info">
208266
<h3>RootService (providedIn: 'root')</h3>
267+
{% raw %}
268+
209269
<p>ID: {{ rootService.getInstanceId() }}</p>
270+
{% endraw %}
271+
210272
<p>{{ rootService.getScope() }}</p>
273+
211274
</div>
212275
213276
<div class="service-info">
214277
<h3>AnyService (providedIn: 'any')</h3>
278+
{% raw %}
279+
215280
<p>ID: {{ anyService.getInstanceId() }}</p>
281+
{% endraw %}
282+
216283
<p>{{ anyService.getScope() }}</p>
284+
217285
</div>
218286
219287
<div class="service-info">
220288
<h3>ComponentScopedService (component providers)</h3>
289+
{% raw %}
290+
221291
<p>ID: {{ componentService.getInstanceId() }}</p>
292+
{% endraw %}
293+
222294
<p>{{ componentService.getScope() }}</p>
295+
223296
</div>
224297
225298
<app-demo-child></app-demo-child>
@@ -234,6 +307,48 @@ export class DemoComponent {
234307
) {}
235308
}
236309
{% raw %}
310+
import { Component } from '@angular/core';
311+
import { RootService } from './root.service';
312+
import { AnyService } from './any.service';
313+
import { ComponentScopedService } from './component-scoped.service';
314+
315+
@Component({
316+
selector: 'app-demo',
317+
standalone: true,
318+
providers: [ComponentScopedService],
319+
template: `
320+
<div class="demo">
321+
<h2>Demonstração de Escopos</h2>
322+
323+
<div class="service-info">
324+
<h3>RootService (providedIn: 'root')</h3>
325+
<p>ID: {{ rootService.getInstanceId() }}</p>
326+
<p>{{ rootService.getScope() }}</p>
327+
</div>
328+
329+
<div class="service-info">
330+
<h3>AnyService (providedIn: 'any')</h3>
331+
<p>ID: {{ anyService.getInstanceId() }}</p>
332+
<p>{{ anyService.getScope() }}</p>
333+
</div>
334+
335+
<div class="service-info">
336+
<h3>ComponentScopedService (component providers)</h3>
337+
<p>ID: {{ componentService.getInstanceId() }}</p>
338+
<p>{{ componentService.getScope() }}</p>
339+
</div>
340+
341+
<app-demo-child></app-demo-child>
342+
</div>
343+
`
344+
})
345+
export class DemoComponent {
346+
constructor(
347+
public rootService: RootService,
348+
public anyService: AnyService,
349+
public componentService: ComponentScopedService
350+
) {}
351+
}
237352
```typescript
238353
import { Component } from '@angular/core';
239354
import { RootService } from './root.service';
@@ -293,9 +408,15 @@ import { ComponentScopedService } from './component-scoped.service';
293408
template: `
294409
<div class="child-demo">
295410
<h3>Componente Filho</h3>
411+
{% raw %}
412+
296413
<p>RootService ID: {{ rootService.getInstanceId() }} (mesmo)</p>
414+
{% endraw %}
415+
297416
<p>AnyService ID: {{ anyService.getInstanceId() }} (mesmo se mesmo módulo)</p>
417+
298418
<p>ComponentScopedService ID: {{ componentService.getInstanceId() }} (novo)</p>
419+
299420
</div>
300421
`
301422
})
@@ -307,6 +428,31 @@ export class DemoChildComponent {
307428
) {}
308429
}
309430
{% raw %}
431+
import { Component } from '@angular/core';
432+
import { RootService } from './root.service';
433+
import { AnyService } from './any.service';
434+
import { ComponentScopedService } from './component-scoped.service';
435+
436+
@Component({
437+
selector: 'app-demo-child',
438+
standalone: true,
439+
providers: [ComponentScopedService],
440+
template: `
441+
<div class="child-demo">
442+
<h3>Componente Filho</h3>
443+
<p>RootService ID: {{ rootService.getInstanceId() }} (mesmo)</p>
444+
<p>AnyService ID: {{ anyService.getInstanceId() }} (mesmo se mesmo módulo)</p>
445+
<p>ComponentScopedService ID: {{ componentService.getInstanceId() }} (novo)</p>
446+
</div>
447+
`
448+
})
449+
export class DemoChildComponent {
450+
constructor(
451+
public rootService: RootService,
452+
public anyService: AnyService,
453+
public componentService: ComponentScopedService
454+
) {}
455+
}
310456
```typescript
311457
import { Component } from '@angular/core';
312458
import { RootService } from './root.service';

0 commit comments

Comments
 (0)