Skip to content

Commit 6452e24

Browse files
feat(component): add error as value to LetDirective's context (#3380)
Closes #3343 BREAKING CHANGES: The `$error` property from `LetDirective`'s view context is a thrown error or `undefined` instead of `true`/`false`. BEFORE: ```ts <p *ngrxLet="obs$; $error as e">{{ e }}</p> ``` - `e` will be `true` when `obs$` emits error event. - `e` will be `false` when `obs$` emits next/complete event. AFTER: ```ts <p *ngrxLet="obs$; $error as e">{{ e }}</p> ``` - `e` will be thrown error when `obs$` emits error event. - `e` will be `undefined` when `obs$` emits next/complete event.
1 parent 0ffed02 commit 6452e24

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

modules/component/spec/let/let.directive.spec.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class LetDirectiveTestComponent {
4747

4848
@Component({
4949
template: `
50-
<ng-container *ngrxLet="value$; $error as error">{{ error }}</ng-container>
50+
<ng-container *ngrxLet="value$; $error as error">{{
51+
error === undefined ? 'undefined' : error
52+
}}</ng-container>
5153
`,
5254
})
5355
class LetDirectiveTestErrorComponent {
@@ -376,21 +378,25 @@ describe('LetDirective', () => {
376378
describe('when error', () => {
377379
beforeEach(waitForAsync(setupLetDirectiveTestComponentError));
378380

379-
it('should render the error to false if next or complete', () => {
380-
letDirectiveTestComponent.value$ = of(1);
381+
it('should render undefined when next event is emitted', () => {
382+
letDirectiveTestComponent.value$ = new BehaviorSubject(1);
381383
fixtureLetDirectiveTestComponent.detectChanges();
382-
expect(componentNativeElement.textContent).toBe('false');
384+
expect(componentNativeElement.textContent).toBe('undefined');
383385
});
384386

385-
it('should render the error to true if one occurs', () => {
386-
letDirectiveTestComponent.value$ = throwError(
387-
() => new Error('error message')
388-
);
387+
it('should render undefined when complete event is emitted', () => {
388+
letDirectiveTestComponent.value$ = EMPTY;
389389
fixtureLetDirectiveTestComponent.detectChanges();
390-
expect(componentNativeElement.textContent).toBe('true');
390+
expect(componentNativeElement.textContent).toBe('undefined');
391+
});
392+
393+
it('should render error when error event is emitted', () => {
394+
letDirectiveTestComponent.value$ = throwError(() => 'error message');
395+
fixtureLetDirectiveTestComponent.detectChanges();
396+
expect(componentNativeElement.textContent).toBe('error message');
391397
});
392398

393-
it('should call error handler', () => {
399+
it('should call error handler when error event is emitted', () => {
394400
const errorHandler = TestBed.inject(ErrorHandler);
395401
const error = new Error('ERROR');
396402
letDirectiveTestComponent.value$ = throwError(() => error);

modules/component/src/let/let.directive.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface LetViewContext<T> {
2626
/**
2727
* `*ngrxLet="obs$; let e = $error"` or `*ngrxLet="obs$; $error as e"`
2828
*/
29-
$error: boolean;
29+
$error: any;
3030
/**
3131
* `*ngrxLet="obs$; let c = $complete"` or `*ngrxLet="obs$; $complete as c"`
3232
*/
@@ -66,7 +66,7 @@ export interface LetViewContext<T> {
6666
* <app-number [number]="n" *ngIf="!e && !c">
6767
* </app-number>
6868
*
69-
* <p *ngIf="e">There is an error.</p>
69+
* <p *ngIf="e">There is an error: {{ e }}</p>
7070
* <p *ngIf="c">Observable is completed.</p>
7171
* </ng-container>
7272
* ```
@@ -94,7 +94,7 @@ export class LetDirective<U> implements OnInit, OnDestroy {
9494
private readonly viewContext: LetViewContext<U | null | undefined> = {
9595
$implicit: undefined,
9696
ngrxLet: undefined,
97-
$error: false,
97+
$error: undefined,
9898
$complete: false,
9999
$suspense: true,
100100
};
@@ -106,7 +106,7 @@ export class LetDirective<U> implements OnInit, OnDestroy {
106106
reset: () => {
107107
this.viewContext.$implicit = undefined;
108108
this.viewContext.ngrxLet = undefined;
109-
this.viewContext.$error = false;
109+
this.viewContext.$error = undefined;
110110
this.viewContext.$complete = false;
111111
this.viewContext.$suspense = true;
112112

@@ -118,14 +118,14 @@ export class LetDirective<U> implements OnInit, OnDestroy {
118118
this.viewContext.$suspense = false;
119119

120120
if (event.reset) {
121-
this.viewContext.$error = false;
121+
this.viewContext.$error = undefined;
122122
this.viewContext.$complete = false;
123123
}
124124

125125
this.renderMainView();
126126
},
127127
error: (event) => {
128-
this.viewContext.$error = true;
128+
this.viewContext.$error = event.error;
129129
this.viewContext.$suspense = false;
130130

131131
if (event.reset) {
@@ -144,7 +144,7 @@ export class LetDirective<U> implements OnInit, OnDestroy {
144144
if (event.reset) {
145145
this.viewContext.$implicit = undefined;
146146
this.viewContext.ngrxLet = undefined;
147-
this.viewContext.$error = false;
147+
this.viewContext.$error = undefined;
148148
}
149149

150150
this.renderMainView();

projects/ngrx.io/content/guide/component/let.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ We can track next, error, and complete events:
5959
<app-number [number]="n" *ngIf="!e && !c">
6060
</app-number>
6161

62-
<p *ngIf="e">There is an error.</p>
62+
<p *ngIf="e">There is an error: {{ e }}</p>
6363
<p *ngIf="c">Observable is completed.</p>
6464
</ng-container>
6565
```

0 commit comments

Comments
 (0)