Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer:8 Angular pure pipe #1107

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions apps/angular/8-pure-pipe/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { NgFor } from '@angular/common';
import { Component } from '@angular/core';
import { HeavyComputation } from './heavy-computaion.pipe';

@Component({
standalone: true,
imports: [NgFor],
imports: [HeavyComputation],
selector: 'app-root',
template: `
<div *ngFor="let person of persons; let index = index">
{{ heavyComputation(person, index) }}
</div>
@for (person of persons; track $index) {
<div>
{{ person | heavyComputation: $index }}
</div>
}
`,
})
export class AppComponent {
persons = ['toto', 'jack'];

heavyComputation(name: string, index: number) {
// very heavy computation
return `${name} - ${index}`;
}
}
11 changes: 11 additions & 0 deletions apps/angular/8-pure-pipe/src/app/heavy-computaion.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'heavyComputation',
standalone: true,
})
export class HeavyComputation implements PipeTransform {
transform(value: string, index: number): string {
return `${value} - ${index}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be on the safer side - can we add a null check here? Everything else is great!

Something like:

transform(name: string | null | undefined, index: number | null | undefined): string {
    if (name == null || index == null) {
      return '';
    }
    
    return `${name} - ${index}`;
  }

}
}
Loading