-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Answer:34 default vs onpush #1398
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,17 @@ | ||
| import { Component } from '@angular/core'; | ||
| import { randFirstName } from '@ngneat/falso'; | ||
| import { PersonListComponent } from './person-list.component'; | ||
| import { PersonComponent } from './person.component'; | ||
| import { RandomComponent } from './random.component'; | ||
|
|
||
| @Component({ | ||
| imports: [PersonListComponent, RandomComponent], | ||
| imports: [RandomComponent, PersonComponent], | ||
| selector: 'app-root', | ||
| template: ` | ||
| <app-random /> | ||
|
|
||
| <div class="flex"> | ||
| <app-person-list [names]="girlList" title="Female" /> | ||
| <app-person-list [names]="boyList" title="Male" /> | ||
| <app-person gender="female" title="Female"></app-person> | ||
| <app-person gender="male" title="Male"></app-person> | ||
| </div> | ||
| `, | ||
| }) | ||
| export class AppComponent { | ||
| girlList = randFirstName({ gender: 'female', length: 10 }); | ||
| boyList = randFirstName({ gender: 'male', length: 10 }); | ||
| } | ||
| export class AppComponent {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { TitleCasePipe } from '@angular/common'; | ||
| import { Component, inject, input, OnInit } from '@angular/core'; | ||
| import { PersonListComponent } from './person-list.component'; | ||
| import { PersonFormComponent } from './person.form.component'; | ||
| import { PersonService } from './person.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-person', | ||
| imports: [TitleCasePipe, PersonListComponent, PersonFormComponent], | ||
| template: ` | ||
| <h1 class="text-center font-semibold" title="Title"> | ||
| {{ title() | titlecase }} | ||
| </h1> | ||
| <person-form (addLabel)="onAddLabel($event)" /> | ||
| <app-person-list [names]="names()" /> | ||
| `, | ||
| host: { | ||
| class: 'w-full flex flex-col items-center', | ||
| }, | ||
| providers: [PersonService], | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you forgot 'changedetection.onpush` |
||
| }) | ||
| export class PersonComponent implements OnInit { | ||
| service = inject(PersonService); | ||
| title = input(''); | ||
| gender = input<'male' | 'female'>('male'); | ||
| names = this.service.list; | ||
|
|
||
| ngOnInit(): void { | ||
| this.service.initList(this.gender()); | ||
| } | ||
|
|
||
| onAddLabel(label: string) { | ||
| this.service.add(label); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { CDFlashingDirective } from '@angular-challenges/shared/directives'; | ||
| import { ChangeDetectionStrategy, Component, output } from '@angular/core'; | ||
| import { FormsModule } from '@angular/forms'; | ||
| import { MatFormFieldModule } from '@angular/material/form-field'; | ||
| import { MatInputModule } from '@angular/material/input'; | ||
|
|
||
| @Component({ | ||
| selector: 'person-form', | ||
| imports: [ | ||
| FormsModule, | ||
| MatFormFieldModule, | ||
| MatInputModule, | ||
| CDFlashingDirective, | ||
| ], | ||
| template: ` | ||
| <mat-form-field class="w-4/5" cd-flash> | ||
| <input | ||
| placeholder="Add one member to the list" | ||
| matInput | ||
| type="text" | ||
| [(ngModel)]="label" | ||
| (keydown)="handleKey($event)" /> | ||
| </mat-form-field> | ||
| `, | ||
| host: { | ||
| class: 'w-full flex flex-col items-center', | ||
| }, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class PersonFormComponent { | ||
| label = ''; | ||
|
||
| addLabel = output<string>(); | ||
|
|
||
| handleKey(event: KeyboardEvent) { | ||
| if (event.key === 'Enter') { | ||
| this.addLabel.emit(this.label); | ||
| this.label = ''; | ||
| } | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Injectable, signal } from '@angular/core'; | ||
| import { randFirstName } from '@ngneat/falso'; | ||
|
|
||
| @Injectable() | ||
| export class PersonService { | ||
| private _list = signal<string[]>([]); | ||
| list = this._list.asReadonly(); | ||
|
|
||
| initList(gender: 'male' | 'female') { | ||
| if (gender === 'male') { | ||
| this._list.set(randFirstName({ gender: 'male', length: 10 })); | ||
| } else { | ||
| this._list.set(randFirstName({ gender: 'female', length: 10 })); | ||
| } | ||
| } | ||
|
|
||
| add(name: string) { | ||
| this._list.update((names) => [...names, name]); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| import { CDFlashingDirective } from '@angular-challenges/shared/directives'; | ||
| import { Component } from '@angular/core'; | ||
| import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-random', | ||
| template: ` | ||
| <div cd-flash>I do nothing but I'm here</div> | ||
| `, | ||
| imports: [CDFlashingDirective], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class RandomComponent {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the @if is useless here, you should use the @empty of the @for.