-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralized state management without BehaviorSubject.
- Loading branch information
Jonas Bandi
committed
Feb 28, 2019
1 parent
7b8e0a0
commit f88f7bb
Showing
3 changed files
with
14 additions
and
51 deletions.
There are no files selected for viewing
25 changes: 6 additions & 19 deletions
25
plain-change-detection/src/app/counter/counter.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,29 @@ | ||
import { Component, OnInit, OnDestroy } from '@angular/core'; | ||
import { Component} from '@angular/core'; | ||
import { CounterService } from '../services/counter.service'; | ||
|
||
@Component({ | ||
selector: 'app-counter', | ||
templateUrl: './counter.component.html', | ||
styleUrls: ['./counter.component.css'] | ||
}) | ||
export class CounterComponent implements OnInit, OnDestroy { | ||
export class CounterComponent { | ||
|
||
constructor(private counter: CounterService) { } | ||
|
||
currentCount: number; | ||
subscription; | ||
|
||
ngOnInit(): void { | ||
this.subscription = this.counter.getCount().subscribe( | ||
res => { | ||
this.currentCount = res.value; | ||
}, | ||
err => { | ||
console.error(`An error occurred: ${err.message}`); | ||
} | ||
); | ||
get currentCount() { | ||
return this.counter.currentCount.value; | ||
} | ||
|
||
increment(): void { | ||
this.counter.setCount(this.currentCount, 1); | ||
this.counter.applyDelta(1); | ||
} | ||
|
||
decrement(): void { | ||
this.counter.setCount(this.currentCount, -1); | ||
this.counter.applyDelta(-1); | ||
} | ||
|
||
reset(): void { | ||
this.counter.resetCount(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.subscription.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
import { Component, OnInit, Input, OnDestroy } from '@angular/core'; | ||
import { Component, Input } from '@angular/core'; | ||
import { CounterService } from '../services/counter.service'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
@Component({ | ||
selector: 'app-output', | ||
templateUrl: './output.component.html', | ||
styleUrls: ['./output.component.css'] | ||
}) | ||
export class OutputComponent implements OnInit, OnDestroy { | ||
export class OutputComponent { | ||
|
||
constructor(private counter: CounterService) { } | ||
|
||
@Input() text: string; | ||
currentCount: number; | ||
subscription; | ||
|
||
ngOnInit(): void { | ||
this.subscription = this.counter.getCount().subscribe( | ||
res => { | ||
this.currentCount = res.value; | ||
}, | ||
err => { | ||
console.error(`An error occurred: ${err.message}`); | ||
} | ||
); | ||
get currentCount() { | ||
return this.counter.currentCount.value; | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.subscription.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters