-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Answer:1 content projection solution #1379
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,9 +1,51 @@ | ||
| import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
| import { CityStore } from '../../data-access/city.store'; | ||
| import { | ||
| FakeHttpService, | ||
| randomCity, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { CardComponent } from '../../ui/card/card.component'; | ||
| import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-city-card', | ||
| template: 'TODO City', | ||
| imports: [], | ||
| template: ` | ||
| <app-card | ||
| [list]="cities()" | ||
| [itemTemplate]="itemTemplate" | ||
| (onAddNewItem)="addNewCity()"> | ||
| <img ngSrc="assets/img/city.png" width="200" height="200" /> | ||
| </app-card> | ||
|
|
||
| <ng-template #itemTemplate let-item> | ||
| <app-list-item | ||
| [name]="item.name" | ||
| [id]="item.id" | ||
| [type]="cardType" | ||
| (onDeleteItem)="deleteCity($event)"></app-list-item> | ||
| </ng-template> | ||
| `, | ||
| imports: [CardComponent, NgOptimizedImage, ListItemComponent], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class CityCardComponent {} | ||
| export class CityCardComponent { | ||
| private http = inject(FakeHttpService); | ||
| private store = inject(CityStore); | ||
|
|
||
| cities = this.store.cities; | ||
| cardType = CardType.CITY; | ||
|
|
||
| ngOnInit(): void { | ||
| this.http.fetchCities$.subscribe((c) => this.store.addAll(c)); | ||
| } | ||
|
|
||
| addNewCity() { | ||
| this.store.addOne(randomCity()); | ||
| } | ||
|
|
||
| deleteCity(id: number) { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,47 @@ | ||
| import { Component, inject, OnInit } from '@angular/core'; | ||
| import { FakeHttpService } from '../../data-access/fake-http.service'; | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| inject, | ||
| OnInit, | ||
| } from '@angular/core'; | ||
| import { | ||
| FakeHttpService, | ||
| randTeacher, | ||
| } from '../../data-access/fake-http.service'; | ||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { CardComponent } from '../../ui/card/card.component'; | ||
| import { ListItemComponent } from '../../ui/list-item/list-item.component'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-teacher-card', | ||
| template: ` | ||
| <app-card | ||
| [list]="teachers()" | ||
| [type]="cardType" | ||
| customClass="bg-light-red"></app-card> | ||
| [itemTemplate]="itemTemplate" | ||
| class="bg-light-red" | ||
| (onAddNewItem)="addNewTeacher()"> | ||
| <img priority ngSrc="assets/img/teacher.png" width="200" height="200" /> | ||
| </app-card> | ||
|
|
||
| <ng-template #itemTemplate let-item> | ||
| <app-list-item | ||
| [name]="item.firstName" | ||
| [id]="item.id" | ||
| [type]="cardType" | ||
| (onDeleteItem)="deleteTeacher($event)"></app-list-item> | ||
|
||
| </ng-template> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| ::ng-deep .bg-light-red { | ||
| .bg-light-red { | ||
| background-color: rgba(250, 0, 0, 0.1); | ||
| } | ||
| `, | ||
| ], | ||
| imports: [CardComponent], | ||
| imports: [CardComponent, NgOptimizedImage, ListItemComponent], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class TeacherCardComponent implements OnInit { | ||
| private http = inject(FakeHttpService); | ||
|
|
@@ -31,4 +53,12 @@ export class TeacherCardComponent implements OnInit { | |
| ngOnInit(): void { | ||
| this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t)); | ||
| } | ||
|
|
||
| addNewTeacher() { | ||
| this.store.addOne(randTeacher()); | ||
| } | ||
|
|
||
| deleteTeacher(id: number) { | ||
| this.store.deleteOne(id); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { signal } from '@angular/core'; | ||
|
|
||
| export abstract class Store<T> { | ||
| protected readonly items = signal<T[]>([]); | ||
|
|
||
| abstract addAll(items: T[]): void; | ||
| abstract addOne(item: T): void; | ||
| abstract deleteOne(id: number): void; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,36 @@ | ||
| import { NgOptimizedImage } from '@angular/common'; | ||
| import { Component, inject, input } from '@angular/core'; | ||
| import { randStudent, randTeacher } from '../../data-access/fake-http.service'; | ||
| import { StudentStore } from '../../data-access/student.store'; | ||
| import { TeacherStore } from '../../data-access/teacher.store'; | ||
| import { CardType } from '../../model/card.model'; | ||
| import { ListItemComponent } from '../list-item/list-item.component'; | ||
| import { NgTemplateOutlet } from '@angular/common'; | ||
| import { Component, input, output, TemplateRef } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-card', | ||
| template: ` | ||
| <div | ||
| class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4" | ||
| [class]="customClass()"> | ||
| @if (type() === CardType.TEACHER) { | ||
| <img ngSrc="assets/img/teacher.png" width="200" height="200" /> | ||
| } | ||
| @if (type() === CardType.STUDENT) { | ||
| <img ngSrc="assets/img/student.webp" width="200" height="200" /> | ||
| } | ||
| <ng-content select="img"></ng-content> | ||
| <section> | ||
| @for (item of list(); track item) { | ||
| <app-list-item | ||
| [name]="item.firstName" | ||
| [id]="item.id" | ||
| [type]="type()"></app-list-item> | ||
| } | ||
| </section> | ||
| <section> | ||
| @for (item of list(); track item) { | ||
| <ng-container | ||
| [ngTemplateOutlet]="itemTemplate()" | ||
| [ngTemplateOutletContext]="{ $implicit: item }"></ng-container> | ||
| } | ||
| </section> | ||
| <button | ||
| class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
| (click)="addNewItem()"> | ||
| Add | ||
| </button> | ||
| </div> | ||
| <button | ||
| class="rounded-sm border border-blue-500 bg-blue-300 p-2" | ||
| (click)="addNewItem()"> | ||
| Add | ||
| </button> | ||
| `, | ||
| imports: [ListItemComponent, NgOptimizedImage], | ||
| host: { | ||
| class: 'border-2 border-black rounded-md p-4 w-fit flex flex-col gap-3', | ||
| }, | ||
| imports: [NgTemplateOutlet], | ||
| }) | ||
| export class CardComponent { | ||
| private teacherStore = inject(TeacherStore); | ||
| private studentStore = inject(StudentStore); | ||
|
|
||
| readonly list = input<any[] | null>(null); | ||
| readonly type = input.required<CardType>(); | ||
| readonly customClass = input(''); | ||
|
|
||
| CardType = CardType; | ||
| export class CardComponent<T> { | ||
| readonly list = input<T[] | null>(null); | ||
| readonly itemTemplate = input<TemplateRef<any> | null>(null); | ||
|
||
| onAddNewItem = output(); | ||
|
|
||
| addNewItem() { | ||
| const type = this.type(); | ||
| if (type === CardType.TEACHER) { | ||
| this.teacherStore.addOne(randTeacher()); | ||
| } else if (type === CardType.STUDENT) { | ||
| this.studentStore.addOne(randStudent()); | ||
| } | ||
| this.onAddNewItem.emit(); | ||
| } | ||
| } | ||
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.
you can now use new angular API like rxRessource to fetch data and being declarative