Angular Philosophies

Angular 22 · TypeScript 6.0 · signals · NgRx SignalStore

خلاصة خبرة السنين في Angular، جنبك وإنت شغال.

Angular Philosophies هو الطريقة اللي بكتب بيها Angular، متكتبة: اتناشر فصل تقراهم، ونفس النصايح كـ 132 قاعدة يمشي عليها الـ AI اللي بتستخدمه وهو شغال. لـ Angular 22.

ابدأ استخدامه اتفرج على مراجعة

مطلوب تعديلات Add orders list page with filters #482 فتحه مساعد الـ AI · ملف واحد اتغيّر order-list.component.ts+377 تعليقات مراجعة

خلّي الـ AI يشتغل بيه.

اختار الأداة. كل سطر بيضيف نفس القواعد.

الـ skill و MCP server في خطوة واحدة. الـ skill بيتحمّل لما تشتغل على Angular.

/plugin marketplace add tomavic/angular-philosophies
/plugin install angular-philosophies@angular-philosophies

لو مشروعك ليه قواعده، اكتبها فوق القواعد المتولّدة. المشروع بيكسب على الدليل.

أو اقراه بنفسك.

اتناشر فصل، كل واحد بيخلص بقواعده. وقتك ضيق؟ ابدأ بـ 03.

09:12الصبح، أول اليوم

بتطلب من الـ AI يعملك صفحة.

بتفتح Claude Code أو Cursor أو Copilot، وتكتب سطر واحد: عايز صفحة طلبات فيها بحث وتقسيم صفحات. نوع الصفحة اللي في كل app منها عشرة.

إنت ← للـ AI

Add an orders list page with filters.

09:13بعد دقيقة

الـ AI بيرد في ثواني. الكود بيعمل compile، واللستة بتظهر.

غالبًا كنت هتعمله merge. أغلب كود Angular على الإنترنت شكله كده، فده اللي الموديل بيتعلم يكتبه.

09:14بعد دقيقتين

وبعدين أنا براجعه. 7 تعليقات.

كل تعليق بيقتبس قاعدة من الدليل بالحرف، وبيقول إيه اللي هيبوظ لو اتجاهلتها.

Angular Philosophiesسطر 4 · 1 من 7

path بيتكسر لما فولدر يتنقل.

Import through path aliases (@shop/..., @/...); never use ../../ across feature or library boundaries.

التلات مستويات ../ دي بتقول الملف قاعد فين، مش هو إيه. انقل الـ feature وكل import زي ده هيتكسر.

01 · المعمارية

Angular Philosophiesسطر 16 · 2 من 7

inject من الـ constructor.

Inject dependencies with inject() in field initialisers; do not use constructors for injection.

طريقة واحدة للـ inject في كل حتة: الـ components والـ functions والـ store features كلها بتستخدم inject()، ومفيش حاجة معتمدة على ترتيب الـ constructor.

02 · الـ Components

Angular Philosophiesسطر 11 · 3 من 7

undefined لحد ما السيرفر يرد.

Give every resource a defaultValue and { errorHandling: 'previous value' }; never write ?. or ?? on a resource value in a component.

فالـ template بيتملي ?. والـ empty state بيظهر لحظة قبل أول صفحة.

04 · الوصول للبيانات

Angular Philosophiesسطور 2، 16، 29، 30 · 4 من 7

الصفحة بتكلّم السيرفر بنفسها.

Make a signalStore the facade, API layer and state layer for its slice; components never inject HttpClient or a data service.

كده الـ component شايل الـ URL والـ params والـ error handling، والصفحة الجاية هتنسخ التلاتة.

03 · الـ signal store

Angular Philosophiesسطور 3، 14، 28، 31، 34، 35 · 5 من 7

subscription لازم تفضل ماسكها بإيدك.

Never call .subscribe() in a component; use signals, resources or rxMethod.

كل subscribe هو leak مستني unsubscribe اتنسى. ودي محتاجة تتقفل في مكانين أصلًا.

03 · الـ signal store

Angular Philosophiesسطور 19، 24، 27 · 6 من 7

load() لازم حد يفتكرها.

Never add a load() method that a component must call after a filter changes; derive reads from state with resources.

ضيف فلتر status بكرة، انسى نداء واحد، واللستة هتفضل تعرض النتيجة القديمة من غير ما حد ياخد باله.

03 · الـ signal store

Angular Philosophiesسطور 22، 23 · 7 من 7

البحث بيسيبك في صفحة 3.

Reset page to 1 in every filter setter.

ابحث وإنت في صفحة 3، والسيرفر هيرجّع صفحة 3 من النتايج. غالبًا فاضية، فاليوزر هيفتكر إن مفيش حاجة طابقت. ودي bug حقيقية.

03 · الـ signal store

تصليح السبعة بإيدك ياخد بعد الضهر كله.

أو الـ AI يقرا الدليل قبل ما يكتب أول سطر.

09:20بعد 8 دقايق من أول طلب

نفس الطلب، بس المرة دي مع الدليل.

الـ component بيصغر لحد ما يبقى اللي الصفحة المفروض تكونه: store وtemplate. كل اللي كان بيعمله بإيده راح لـ store بيقرا من الـ state. من 9:12 لـ 9:20: تمان دقايق، ومفيش حاجة محتاجة تتصلّح.

order-list.component.ts
import { Component, OnDestroy, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Subscription } from 'rxjs';
import { Order } from '../../../shared/models/order';
import { Component, inject } from '@angular/core';
import { OrderListStore } from './order-list.store';
import { ORDER_STATUS_OPTIONS } from './order.model';
@Component({
selector: 'app-order-list',
templateUrl: './order-list.component.html',
providers: [OrderListStore],
host: { class: 'block p-4' },
})
export class OrderListComponent implements OnInit, OnDestroy {
export class OrderListComponent {
orders?: Order[];
page = 1;
search = '';
private sub?: Subscription;
constructor(private http: HttpClient) {}
ngOnInit() {
this.load();
}
onSearch(search: string) {
this.search = search;
this.load();
}
load() {
this.sub?.unsubscribe();
this.sub = this.http
.get<Order[]>(`/api/orders?page=${this.page}&q=${this.search}`)
.subscribe(orders => (this.orders = orders));
}
ngOnDestroy() {
this.sub?.unsubscribe();
}
protected readonly store = inject(OrderListStore);
protected readonly statuses = ORDER_STATUS_OPTIONS;
}
  • 14سطر بس في الـ component، بدل 37.
  • مفيشsubscribe في الـ component. كان فيه 1.
  • مفيشload() في الـ component. كان فيه 3.
  • مفيشHttpClient في الـ component. كان فيه 2.
افتح الـ store اللي كتبه

ده الـ store اللي الـ AI بيكتبه وهو ماشي على الدليل. ونفس الكود موجود في الدليل نفسه ومتجرّب: بيعمل compile، وليه tests بتعدّي.

1import { httpResource } from '@angular/common/http';
2import { computed } from '@angular/core';
3import { toObservable, toSignal } from '@angular/core/rxjs-interop';
4import { patchState, signalStore, withComputed, withMethods, withProps, withState } from '@ngrx/signals';
5import { httpMutation, withMutations, withResource } from '@ngrx-toolkit/core';
6import { debounceTime } from 'rxjs';
7import { guardedDevtools } from './guarded-devtools';
8import { Order, OrderStatus, Page } from './order.model';
9
10interface OrderListState {
11 page: number;
12 search: string;
13 status: OrderStatus | null;
14}
15
16const initialState: OrderListState = { page: 1, search: '', status: null };
17const EMPTY_PAGE: Page<Order> = { items: [], total: 0 };
18
19export const OrderListStore = signalStore(
20 guardedDevtools('orderList'),
21 withState(initialState),
22 // A debounced copy of the search box. Derived state stays a signal, never a subscription.
23 withProps(store => ({
24 _debouncedSearch: toSignal(toObservable(store.search).pipe(debounceTime(300)), { initialValue: '' }),
25 })),
26 // Reads are resources derived from state: changing a filter re-derives the request, nobody calls load().
27 withResource(
28 store => ({
29 list: httpResource<Page<Order>>(
30 () => ({
31 url: '/api/orders',
32 params: {
33 page: store.page(),
34 ...(store._debouncedSearch() ? { search: store._debouncedSearch() } : {}),
35 ...(store.status() ? { status: store.status()! } : {}),
36 },
37 }),
38 { defaultValue: EMPTY_PAGE },
39 ),
40 }),
41 { errorHandling: 'previous value' },
42 ),
43 withMutations(store => ({
44 cancel: httpMutation({
45 request: (id: number) => ({ url: `/api/orders/${id}/cancel`, method: 'POST', body: {} }),
46 onSuccess: () => store._listReload(),
47 }),
48 })),
49 withComputed(store => ({
50 pageCount: computed(() => Math.max(1, Math.ceil(store.listValue().total / 20))),
51 })),
52 withMethods(store => ({
53 setSearch(search: string) {
54 patchState(store, { search, page: 1 });
55 },
56 setStatus(status: OrderStatus | null) {
57 patchState(store, { status, page: 1 });
58 },
59 setPage(page: number) {
60 patchState(store, { page });
61 },
62 /** Refetch the same request. A failed reload keeps the last good value; see docs/04-data-access.md. */
63 refresh() {
64 store._listReload();
65 },
66 reset() {
67 patchState(store, initialState);
68 },
69 })),
70);

خلاصة خبرة السنين في شكل جديد، تقدر تستعمله مع الـ AI، وكأني أنا موجود معاك وبقولك على كل النصايح وإنت شغال.