mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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:
parent
0a56d82900
commit
ea4acaf9eb
1 changed files with 3 additions and 3 deletions
|
|
@ -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];
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue