docs(core): Update linked-signal.md (#58756)

fix multiple issues with the existing code
1. `shippingOptions` was accessed without `this.` which produces error in realtime
2. the logic in calculating of the item exist or not was not correct since it was comparing the object to the id
3. I'm not sure why, but step 3 fail without stating typing for LinkedSignal, so I added it

PR Close #58756
This commit is contained in:
robertIsaac 2024-11-19 23:33:13 +02:00 committed by Pawel Kozlowski
parent 0a56d82900
commit ea4acaf9eb

View file

@ -63,13 +63,13 @@ In the example above, `selectedOption` always updates back to the first option w
export class ShippingMethodPicker {
shippingOptions: Signal<ShippingMethod[]> = getShippingOptions();
selectedOption = linkedSignal({
selectedOption = linkedSignal<ShippingMethod[], ShippingMethod>({
// `selectedOption` is set to the `computation` result whenever this `source` changes.
source: shippingOptions,
source: this.shippingOptions,
computation: (newOptions, previous) => {
// If the newOptions contain the previously selected option, preserve that selection.
// Otherwise, default to the first option.
return newOptions.find(opt => opt.id === previous?.value) ?? newOptions[0];
return newOptions.find(opt => opt.id === previous?.value?.id) ?? newOptions[0];
}
});