W3cubDocs

/Angular

toSignal

function

toSignal<T, U = undefined>(source: Observable<T> | Subscribable<T>, options?: ToSignalOptions<U>): Signal<T | U>

Parameters
source Observable<T> | Subscribable<T>
options ToSignalOptions<U>

Optional. Default is undefined.

Returns

Signal<T | U>

Overloads

Get the current value of an Observable as a reactive Signal.

toSignal(source: Observable<T> | Subscribable<T>): Signal<T | undefined>

Parameters
source Observable<T> | Subscribable<T>
Returns

Signal<T | undefined>

toSignal returns a Signal which provides synchronous reactive access to values produced by the given Observable, by subscribing to that Observable. The returned Signal will always have the most recent value emitted by the subscription, and will throw an error if the Observable errors.

Before the Observable emits its first value, the Signal will return undefined. To avoid this, either an initialValue can be passed or the requireSync option enabled.

By default, the subscription will be automatically cleaned up when the current injection context is destroyed. For example, when toObservable is called during the construction of a component, the subscription will be cleaned up when the component is destroyed. If an injection context is not available, an explicit Injector can be passed instead.

If the subscription should persist until the Observable itself completes, the manualCleanup option can be specified instead, which disables the automatic subscription teardown. No injection context is needed in this configuration as well.

Get the current value of an Observable as a reactive Signal.

toSignal(source: Observable<T> | Subscribable<T>, options?: ToSignalOptions<undefined> & { requireSync?: false; }): Signal<T | undefined>

Parameters
source Observable<T> | Subscribable<T>
options ToSignalOptions<undefined> & { requireSync?: false; }

Optional. Default is undefined.

Returns

Signal<T | undefined>

toSignal returns a Signal which provides synchronous reactive access to values produced by the given Observable, by subscribing to that Observable. The returned Signal will always have the most recent value emitted by the subscription, and will throw an error if the Observable errors.

Before the Observable emits its first value, the Signal will return the configured initialValue, or undefined if no initialValue is provided. If the Observable is guaranteed to emit synchronously, then the requireSync option can be passed instead.

By default, the subscription will be automatically cleaned up when the current injection context is destroyed. For example, when toObservable is called during the construction of a component, the subscription will be cleaned up when the component is destroyed. If an injection context is not available, an explicit Injector can be passed instead.

If the subscription should persist until the Observable itself completes, the manualCleanup option can be specified instead, which disables the automatic subscription teardown. No injection context is needed in this configuration as well.

Get the current value of an Observable as a reactive Signal.

toSignal(source: Observable<T> | Subscribable<T>, options: ToSignalOptions<U> & { initialValue: U; requireSync?: false; }): Signal<T | U>

Parameters
source Observable<T> | Subscribable<T>
options ToSignalOptions<U> & { initialValue: U; requireSync?: false; }
Returns

Signal<T | U>

toSignal returns a Signal which provides synchronous reactive access to values produced by the given Observable, by subscribing to that Observable. The returned Signal will always have the most recent value emitted by the subscription, and will throw an error if the Observable errors.

Before the Observable emits its first value, the Signal will return the configured initialValue. If the Observable is guaranteed to emit synchronously, then the requireSync option can be passed instead.

By default, the subscription will be automatically cleaned up when the current injection context is destroyed. For example, when toObservable is called during the construction of a component, the subscription will be cleaned up when the component is destroyed. If an injection context is not available, an explicit Injector can be passed instead.

If the subscription should persist until the Observable itself completes, the manualCleanup option can be specified instead, which disables the automatic subscription teardown. No injection context is needed in this configuration as well.

Get the current value of an Observable as a reactive Signal.

toSignal(source: Observable<T> | Subscribable<T>, options: ToSignalOptions<undefined> & { requireSync: true; }): Signal<T>

Parameters
source Observable<T> | Subscribable<T>
options ToSignalOptions<undefined> & { requireSync: true; }
Returns

Signal<T>

toSignal returns a Signal which provides synchronous reactive access to values produced by the given Observable, by subscribing to that Observable. The returned Signal will always have the most recent value emitted by the subscription, and will throw an error if the Observable errors.

With requireSync set to true, toSignal will assert that the Observable produces a value immediately upon subscription. No initialValue is needed in this case, and the returned signal does not include an undefined type.

By default, the subscription will be automatically cleaned up when the current injection context is destroyed. For example, when toObservable is called during the construction of a component, the subscription will be cleaned up when the component is destroyed. If an injection context is not available, an explicit Injector can be passed instead.

If the subscription should persist until the Observable itself completes, the manualCleanup option can be specified instead, which disables the automatic subscription teardown. No injection context is needed in this configuration as well.

© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/core/rxjs-interop/toSignal