@@ -3,24 +3,24 @@ import {
33 Component ,
44 computed ,
55 input ,
6+ linkedSignal ,
67} from '@angular/core' ;
7- import { toSignal } from '@angular/core/rxjs-interop' ;
8- import { FormControl , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
8+ import { FormsModule } from '@angular/forms' ;
99import { RouterLink } from '@angular/router' ;
1010import { products } from './products' ;
1111
1212@Component ( {
1313 selector : 'app-order' ,
14- imports : [ RouterLink , ReactiveFormsModule ] ,
14+ imports : [ RouterLink , FormsModule ] ,
1515 template : `
1616 <h2 class="mb-5 w-full bg-gray-400 p-2 text-white">Order</h2>
1717 <section class="flex flex-col gap-5">
18- <form class="flex items-center justify-between gap-5" [formGroup]="form ">
18+ <div class="flex items-center justify-between gap-5">
1919 <label for="countries" class="mb-2 block text-nowrap text-gray-900">
2020 Select a quantity
2121 </label>
2222 <select
23- formControlName="quantity "
23+ [(ngModel)]="selectedQuantity "
2424 id="countries"
2525 class="block w-32 rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500">
2626 <option value="1">1</option>
@@ -29,7 +29,7 @@ import { products } from './products';
2929 <option value="4">4</option>
3030 <option value="5">5</option>
3131 </select>
32- </form >
32+ </div >
3333 <div class="flex justify-between">
3434 <div>SubTotal</div>
3535 <div>{{ totalWithoutVat() }} €</div>
@@ -44,7 +44,7 @@ import { products } from './products';
4444 </div>
4545 <button
4646 routerLink="/checkout"
47- [queryParams]="{ quantity: quantity () }"
47+ [queryParams]="{ quantity: selectedQuantity () }"
4848 queryParamsHandling="merge"
4949 class="w-full rounded-full border bg-blue-500 p-2 text-white">
5050 Checkout
@@ -54,18 +54,18 @@ import { products } from './products';
5454 changeDetection : ChangeDetectionStrategy . OnPush ,
5555} )
5656export default class OrderComponent {
57- form = new FormGroup ( {
58- quantity : new FormControl ( 1 , { nonNullable : true } ) ,
59- } ) ;
60-
57+ quantity = input ( '1' ) ;
6158 productId = input ( '1' ) ;
59+
60+ selectedQuantity = linkedSignal ( ( ) => {
61+ return this . quantity ( ) ?? 1 ;
62+ } ) ;
6263 price = computed (
6364 ( ) => products . find ( ( p ) => p . id === this . productId ( ) ) ?. price ?? 0 ,
6465 ) ;
65- quantity = toSignal ( this . form . controls . quantity . valueChanges , {
66- initialValue : this . form . getRawValue ( ) . quantity ,
67- } ) ;
68- totalWithoutVat = computed ( ( ) => Number ( this . price ( ) ) * this . quantity ( ) ) ;
66+ totalWithoutVat = computed (
67+ ( ) => Number ( this . price ( ) ) * Number ( this . selectedQuantity ( ) ) ,
68+ ) ;
6969 vat = computed ( ( ) => this . totalWithoutVat ( ) * 0.21 ) ;
7070 total = computed ( ( ) => this . totalWithoutVat ( ) + this . vat ( ) ) ;
7171}
0 commit comments