diff --git a/apps/angular-intro/src/app/app.component.html b/apps/angular-intro/src/app/app.component.html index 6bbfba1..5207a14 100644 --- a/apps/angular-intro/src/app/app.component.html +++ b/apps/angular-intro/src/app/app.component.html @@ -1,4 +1,6 @@ -
+
Hello world + +
diff --git a/apps/angular-intro/src/app/app.component.ts b/apps/angular-intro/src/app/app.component.ts index 1a32f2b..de388db 100644 --- a/apps/angular-intro/src/app/app.component.ts +++ b/apps/angular-intro/src/app/app.component.ts @@ -1,11 +1,12 @@ import { Component } from '@angular/core'; import { CounterComponent } from '../counter/counter.component'; +import { CounterWithServiceComponent } from '../counter-with-service/counter-with-service.component'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrl: './app.component.scss', - imports: [CounterComponent], + imports: [CounterComponent, CounterWithServiceComponent], }) export class AppComponent { title = 'angular-intro'; diff --git a/apps/angular-intro/src/counter-with-service/counter-with-service.component.html b/apps/angular-intro/src/counter-with-service/counter-with-service.component.html new file mode 100644 index 0000000..3fcd7a9 --- /dev/null +++ b/apps/angular-intro/src/counter-with-service/counter-with-service.component.html @@ -0,0 +1,7 @@ +
+

+ +

+ +1 + -1 +
diff --git a/apps/angular-intro/src/counter-with-service/counter-with-service.component.ts b/apps/angular-intro/src/counter-with-service/counter-with-service.component.ts new file mode 100644 index 0000000..81a9969 --- /dev/null +++ b/apps/angular-intro/src/counter-with-service/counter-with-service.component.ts @@ -0,0 +1,26 @@ +import { Component, inject } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +import { ValueComponent } from '../counter/value/value.component'; +import { ButtonComponent } from '../counter/button/button.component'; +import { CounterService } from './counter.service'; + +@Component({ + standalone: true, + selector: 'app-counter-with-service', + templateUrl: './counter-with-service.component.html', + imports: [CommonModule, ValueComponent, ButtonComponent], +}) +export class CounterWithServiceComponent { + private readonly counterService = inject(CounterService); + + count$ = this.counterService.getValue(); + + increment() { + this.counterService.increment(); + } + + decrement() { + this.counterService.decrement(); + } +} diff --git a/apps/angular-intro/src/counter-with-service/counter.service.ts b/apps/angular-intro/src/counter-with-service/counter.service.ts new file mode 100644 index 0000000..44a6af0 --- /dev/null +++ b/apps/angular-intro/src/counter-with-service/counter.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import { BehaviorSubject } from 'rxjs'; + +@Injectable({ providedIn: 'root' }) +export class CounterService { + private value$ = new BehaviorSubject(0); + + getValue() { + return this.value$.asObservable(); + } + + increment() { + this.value$.next(this.value$.getValue() + 1); + } + + decrement() { + this.value$.next(this.value$.getValue() - 1); + } +}