Skip to content
Open
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
16 changes: 14 additions & 2 deletions apps/signal/56-forms-and-signal/src/app/order.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChangeDetectionStrategy,
Component,
computed,
effect,
input,
} from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
Expand Down Expand Up @@ -59,13 +60,24 @@ export default class OrderComponent {
});

productId = input('1');
quantity = input(1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use this.route.snapshot.queryParams['quantity'] since this.route is created first the value is available when you want to instantiate the class,
The code is simpler. but your code works. Waiting for signal forms to make all of this easy 🔥

price = computed(
() => products.find((p) => p.id === this.productId())?.price ?? 0,
);
quantity = toSignal(this.form.controls.quantity.valueChanges, {

quantityValue = toSignal(this.form.controls.quantity.valueChanges, {
initialValue: this.form.getRawValue().quantity,
});
totalWithoutVat = computed(() => Number(this.price()) * this.quantity());

totalWithoutVat = computed(() => Number(this.price()) * this.quantityValue());
vat = computed(() => this.totalWithoutVat() * 0.21);
total = computed(() => this.totalWithoutVat() + this.vat());

constructor() {
effect(() => {
const quantityFromUrl = this.quantity();
this.form.controls.quantity.setValue(quantityFromUrl);
});
}

}