Skip to content

Answer:1 content projection angular #1307

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

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import {
FakeHttpService,
randomCity,
} from '../../data-access/fake-http.service';
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()">
<img
ngProjectAs="card-img"
src="assets/img/city.png"
width="200"
height="200" />
<ng-template #listTemplate let-city>
<app-list-item>
{{ city.name }}
<button (click)="deleteCity(city.id)" ngProjectAs="delete-button">
<img class="h-5" src="assets/svg/trash.svg" alt="icon trash" />
</button>
</app-list-item>
</ng-template>
<button
(click)="addCity()"
ngProjectAs="add-button"
class="rounded-sm border border-blue-500 bg-blue-300 p-2">
Add
</button>
</app-card>
`,
imports: [CardComponent, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CityCardComponent {}
export class CityCardComponent {
private http = inject(FakeHttpService);
private store = inject(CityStore);

cities = this.store.cities;

ngOnInit(): void {
this.http.fetchCities$.subscribe((s) => this.store.addAll(s));
}

deleteCity(id: number) {
this.store.deleteOne(id);
}
addCity() {
this.store.addOne(randomCity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,43 @@ import {
Component,
inject,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import {
FakeHttpService,
randStudent,
} from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.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-student-card',
template: `
<app-card
[list]="students()"
[type]="cardType"
customClass="bg-light-green" />
<app-card [list]="students()" customClass="bg-light-green">
<img
ngProjectAs="card-img"
src="assets/img/student.webp"
width="200"
height="200" />
<ng-template #listTemplate let-student>
<app-list-item>
{{ student.firstName }}
<button
(click)="deleteStudent(student.id)"
ngProjectAs="delete-button">
<img class="h-5" src="assets/svg/trash.svg" alt="icon trash" />
</button>
</app-list-item>
</ng-template>
<button
(click)="addStudent()"
ngProjectAs="add-button"
class="rounded-sm border border-blue-500 bg-blue-300 p-2">
Add
</button>
</app-card>
`,
styles: [
`
Expand All @@ -24,7 +48,8 @@ import { CardComponent } from '../../ui/card/card.component';
}
`,
],
imports: [CardComponent],
encapsulation: ViewEncapsulation.None,
imports: [CardComponent, ListItemComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StudentCardComponent implements OnInit {
Expand All @@ -37,4 +62,11 @@ export class StudentCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchStudents$.subscribe((s) => this.store.addAll(s));
}
deleteStudent(id: number) {
this.store.deleteOne(id);
}

addStudent() {
this.store.addOne(randStudent());
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import { Component, inject, OnInit } from '@angular/core';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { Component, inject, OnInit, ViewEncapsulation } 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>
<app-card [list]="teachers()" customClass="bg-light-red">
<img
ngProjectAs="card-img"
src="assets/img/teacher.png"
width="200"
height="200" />
<ng-template #listTemplate let-teacher>
<app-list-item>
{{ teacher.firstName }}
<button
(click)="deleteTeacher(teacher.id)"
ngProjectAs="delete-button">
<img class="h-5" src="assets/svg/trash.svg" alt="icon trash" />
</button>
</app-list-item>
</ng-template>
<button
(click)="addTeacher()"
ngProjectAs="add-button"
class="rounded-sm border border-blue-500 bg-blue-300 p-2">
Add
</button>
</app-card>
`,
styles: [
`
::ng-deep .bg-light-red {
.bg-light-red {
background-color: rgba(250, 0, 0, 0.1);
}
`,
],
imports: [CardComponent],
encapsulation: ViewEncapsulation.None,
imports: [CardComponent, ListItemComponent],
})
export class TeacherCardComponent implements OnInit {
private http = inject(FakeHttpService);
Expand All @@ -31,4 +55,11 @@ export class TeacherCardComponent implements OnInit {
ngOnInit(): void {
this.http.fetchTeachers$.subscribe((t) => this.store.addAll(t));
}
deleteTeacher(id: number) {
this.store.deleteOne(id);
}

addTeacher() {
this.store.addOne(randTeacher());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { City } from '../model/city.model';
providedIn: 'root',
})
export class CityStore {
private cities = signal<City[]>([]);
public cities = signal<City[]>([]);

addAll(cities: City[]) {
this.cities.set(cities);
Expand Down
46 changes: 9 additions & 37 deletions apps/angular/1-projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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 { NgTemplateOutlet } from '@angular/common';
import { Component, ContentChild, input, TemplateRef } from '@angular/core';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
Expand All @@ -12,47 +8,23 @@ import { ListItemComponent } from '../list-item/list-item.component';
<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="card-img"></ng-content>

<section>
@for (item of list(); track item) {
<app-list-item
[name]="item.firstName"
[id]="item.id"
[type]="type()"></app-list-item>
<ng-container
[ngTemplateOutlet]="listTemplate"
[ngTemplateOutletContext]="{ $implicit: item }"></ng-container>
}
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
<ng-content select="add-button"></ng-content>
</div>
`,
imports: [ListItemComponent, NgOptimizedImage],
imports: [NgTemplateOutlet, ListItemComponent],
})
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;

addNewItem() {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
}
@ContentChild('listTemplate') listTemplate!: TemplateRef<ListItemComponent>;
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
import {
ChangeDetectionStrategy,
Component,
inject,
input,
} from '@angular/core';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
selector: 'app-list-item',
template: `
<div class="border-grey-300 flex justify-between border px-2 py-1">
{{ name() }}
<button (click)="delete(id())">
<img class="h-5" src="assets/svg/trash.svg" />
</button>
<ng-content></ng-content>
<ng-content select="delete-button"></ng-content>
</div>
`,
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListItemComponent {
private teacherStore = inject(TeacherStore);
private studentStore = inject(StudentStore);

readonly id = input.required<number>();
readonly name = input.required<string>();
readonly type = input.required<CardType>();

delete(id: number) {
const type = this.type();
if (type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
}
}
constructor() {}
}
Loading