Skip to content

Answer:56 forms and signal #1169

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 2 commits into
base: main
Choose a base branch
from
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);
});
}

}