Skip to content

Commit b2aa1a5

Browse files
author
mivanov
committed
feat(change-detection-strategy-onpush-no-zone): bugfix
1 parent c9e2331 commit b2aa1a5

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ Example: https://stackblitz.com/github/Angular-RU/change-detection-tree/tree/onp
3030
$ ng serve --app 1 --port 4201
3131
```
3232

33+
- **ChangeDetection.OnPush + Async pipe - without ngZone (random generate tree)**: <br>
34+
Example: https://stackblitz.com/github/Angular-RU/change-detection-tree/tree/onpush-async-without-zone
35+
36+
![](https://habrastorage.org/webt/jq/0t/_l/jq0t_ltli9iyvjtuvumct6awfmk.gif)
37+
38+
```bash
39+
$ ng serve --app 2 --port 4202
40+
```
41+
42+
- **ChangeDetection.Default + Async pipe + ngZone (random generate tree)**: <br>
43+
Example: https://stackblitz.com/github/Angular-RU/change-detection-tree/tree/async-pipe
44+
45+
46+
```bash
47+
$ ng serve --app 3 --port 4203
48+
```
49+
50+
- **Custom state-management (services)**: <br>
51+
Example: In progress
52+
53+
- **NgRx**: <br>
54+
Example: In progress
55+
56+
- **MobX**: <br>
57+
Example: In progress
58+
59+
- **Web-worker platform**: <br>
60+
Example: In progress
61+
3362
#### Detect problem with Zone
3463

3564
> Copy the code and paste it into the console.

change-detection-strategy-onpush-no-zone/app/app.component.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
<tree>
77
<div>
88
<div class="center">
9-
count = {{ countTree$|async|json }}
9+
count$ = {{ countTree$|async|json }}
1010
</div>
1111
</div>
1212

1313
<child
14-
[limit]="{ deep: 6 }"
14+
[limit]="{ deep: 5, limitComponents: { input: 1, button: 1} }"
1515
[count$]="countTree$">
1616
</child>
1717
</tree>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="factorial">
22
<p>FactorialComponent: </p>
3-
<input type="number" [(ngModel)]="factorialValue" min="0"><br>
4-
{{factorialValue}}! = {{factorial(factorialValue)}} <br><br>
3+
<input type="number" [ngModel]="factorialValue" (ngModelChange)="updateValue($event)" min="0"><br>
4+
{{factorialValue}}! = {{factorialValueResult||factorial(factorialValue)}} <br><br>
55
<div #total class="computed"></div>
66
<div #execute class="execute"></div>
77
</div>

change-detection-strategy-onpush-no-zone/app/component/factorial/factorial.component.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ChangeDetectionStrategy, Component, ElementRef, NgZone, ViewChild} from '@angular/core';
1+
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, NgZone, ViewChild} from '@angular/core';
22
import * as math from 'mathjs';
33

44
math.config({number: 'BigNumber', precision: 100});
@@ -11,26 +11,28 @@ math.config({number: 'BigNumber', precision: 100});
1111
})
1212
export class FactorialComponent {
1313
public factorialValue = 10;
14+
public factorialValueResult: number = 0;
1415
private executeCount: number = 0;
1516
@ViewChild('total', {read: ElementRef}) private total: ElementRef;
1617
@ViewChild('execute', {read: ElementRef}) private execute: ElementRef;
1718

18-
constructor(private ngZone: NgZone) {}
19+
constructor(private ngZone: NgZone, private cd: ChangeDetectorRef) {}
20+
21+
public updateValue(val) {
22+
this.factorialValueResult = this.factorial(val);
23+
this.cd.detectChanges();
24+
}
1925

2026
public factorial(n) {
2127
this.executeCount++;
2228
const start = performance.now();
2329
const result = math.eval(`${n}!`).toString();
2430
const total = Math.ceil(performance.now() - start);
2531

26-
this.ngZone.runOutsideAngular(() => {
27-
setTimeout(() => {
28-
if (this.total) {
29-
this.total.nativeElement.setAttribute('data-total', total);
30-
this.execute.nativeElement.setAttribute('data-execute', this.executeCount);
31-
}
32-
}, 2000);
33-
});
32+
if (this.total) {
33+
this.total.nativeElement.setAttribute('data-total', total);
34+
this.execute.nativeElement.setAttribute('data-execute', this.executeCount);
35+
}
3436

3537
return result;
3638
}

change-detection-strategy-onpush-no-zone/app/component/interval/interval.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class IntervalComponent extends TreeNode {
6161
private timerInterval() {
6262
this.timer = window.setInterval(() => {
6363
this.formatDate = this.getFormattedDate();
64+
this.cd.detectChanges();
6465
}, 1000);
6566
}
6667

0 commit comments

Comments
 (0)