
ليه Angular مش بيراقب الـ variables بتاعتك؟ الـ change detection على حقيقته
- كاتب موثّق
- Angular 22
محتويات المقال
console.log اللي في الـ promise callback بيطبع Saved، يعني الـ field فيه القيمة الصح. الشاشة بس عمرها ما بتعرف.
الفكرة الغلط الشائعة
this.status = 'Saved'، والـ DOM هيتحدّث في اللحظة اللي القيمة تتغير فيها.
setTimeout والـ network requests والـ event listeners، كان Angular بيعمل change detection بعد كل واحدة فيهم، فالـ assignment اللي جوه callback كان بيوصل للشاشة في لحظتها تقريبًا. وفيه frameworks تانية فعلًا بتتابع الكتابة في الـ properties، عن طريق proxies أو setters بيعملها الـ compiler، وده بيخلّي الفكرة شكلها عامة. الجزء الصح إن الشاشة فعلًا كانت بتتحدّث بعد الشغل الـ async. بس السبب عمره ما كان watcher، السبب كان zone.js، وتطبيقات Angular 22 مابتحمّلوش بشكل افتراضي.
اللي بيحصل فعلًا
الـ change detection لفّة، مش watcher
this.status = 'Saved' مجرد كتابة عادية في property في JavaScript: لا proxy، ولا setter، ولا أي حاجة بتقول لـ Angular إن ده حصل.
لازم حاجة تطلب اللفّة
listener متربط في الـ template أو الـ host اشتغل، زي (click)؛signal بيقراه template اتحدّث؛ حد نادى markForCheck()، والـasyncpipe بيعملها بدالك؛الـ ComponentRef.setInput()حط input؛view كان اتعمله mark إنه dirty بواحد من اللي فوق اتربط.
setTimeout يشتغل، أو رسالة توصل على WebSocket، ولا واحدة فيهم في اللستة.
الـ OnPush بيحدد أنهي components تتعملها check
ChangeDetectionStrategy.Eager، وده الاسم الجديد لـ Default اللي بقى deprecated.
import { Component, inject } from '@angular/core';@Component({ selector: 'app-save-button', template: ` <button (click)="save()">Save draft</button> <span>{{ status }}</span> `,})export class SaveButton { private readonly drafts = inject(DraftApi); status = ''; save(): void { this.status = 'Saving...'; // shows: the click schedules a pass this.drafts.save().then(() => { this.status = 'Saved'; // never shows: nothing schedules a pass }); }}import { Component, inject, signal } from '@angular/core';@Component({ selector: 'app-save-button', template: ` <button (click)="save()">Save draft</button> <span>{{ status() }}</span> `,})export class SaveButton { private readonly drafts = inject(DraftApi); readonly status = signal(''); save(): void { this.status.set('Saving...'); this.drafts.save().then(() => { this.status.set('Saved'); // marks SaveButton, schedules a pass }); }}status.set('Saved') بيحدّث signal الـ template بيقراه. وده واحد من التنبيهات، فـ Angular بيعلّم SaveButton ويطلب لفّة، والكلمة بتتحدّث.
markForCheck() بيعلّم الـ view والـ ancestors بتوعه، وفي تطبيق zoneless بيطلب اللفّة كمان:
import { ChangeDetectorRef, Component, inject } from '@angular/core';@Component({ selector: 'app-legacy-status', template: `<span>{{ status }}</span>`,})export class LegacyStatus { private readonly cdr = inject(ChangeDetectorRef); private readonly drafts = inject(DraftApi); status = 'Saving...'; constructor() { this.drafts.save().then(() => { this.status = 'Saved'; this.cdr.markForCheck(); // the notification the field can't send }); }}الـ dev mode بيعمل check للـ templates مرتين
ExpressionChangedAfterItHasBeenCheckedError) لو قيمة أي binding اتغيرت ما بين الاتنين. وده الوش التاني لنفس النموذج: Angular بيعرض اللي الـ templates بترجّعه وقت اللفّة، ومتوقع إن قرايتها ماتغيّرش أي حاجة. الـ getter اللي بيرجّع Math.random()، أو child بيغيّر الـ state بتاعة الـ parent وهي بتتعملها check، الاتنين بيوقعوا فيه. والـ production builds مابتعملش الـ check ده.
setTimeout أو subscribe() | |
(click) | |
markForCheck() بعد الـ assignment |
تطبقها إزاي في مشروعك
حوّل الـ fields اللي بتتغير بعد شغل async أي field بيتغير بعد awaitأو.then()أوsetTimeoutأوsubscribe()وبيتقري في template، لازم يبقى signal. وحدّثه بـset()أوupdate().اعرض الـ Observables بالـ async pipe الـ asyncpipe بيناديmarkForCheck()مع كل قيمة جديدة، فالـ stream اللي بيتعرض بيه بيتحدّث من غير كود زيادة.بدّل التعديل في المكان بقيم جديدة بدل items.push(item)، اكتبitems.update((list) => [...list, item])، عشان الـ signal والـ children اللي OnPush يشوفوا التغيير.خلّي markForCheck() للكود القديم في أي component لسه ماتقدرش تنقلها للـ signals، نادي markForCheck()بعد كل assignment بيحصل بره listener في الـ template، وحطها في خطتك عشان تنقلها للـ signals.
الخلاصة
Angular مابيراقبش الـ fields: بيعيد تقييم الـ templates على لفّات ويقارن النتايج بآخر render. في تطبيقات Angular 22، التنبيهات بس هي اللي بتطلب لفّة: signals بتقراها templates، والـ listeners بتاعة الـ template أو الـ host، و markForCheck()، وsetInput().الـ OnPush، الافتراضي من v22، بيقصر كل لفّة على الـ components المتعلّمة بس. الـ state اللي بتتغير بعد شغل async مكانها signal، والـ markForCheck()حل احتياطي، مش هو الحل.
التعليقات (0)