Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

TransferState

A key value store that is transferred from the application on the server side to the application on the client side.

See more...

      
      class TransferState {
  isEmpty: boolean
get<T>(key: StateKey<T>, defaultValue: T): T
set<T>(key: StateKey<T>, value: T): void
remove<T>(key: StateKey<T>): void
hasKey<T>(key: StateKey<T>): boolean
onSerialize<T>(key: StateKey<T>, callback: () => T): void
toJson(): string }

Provided in

  • 'root'

Description

The TransferState is available as an injectable token. On the client, just inject this token using DI and use it, it will be lazily initialized. On the server it's already included if renderApplication function is used. Otherwise, import the ServerTransferStateModule module to make the TransferState available.

The values in the store are serialized/deserialized using JSON.stringify/JSON.parse. So only boolean, number, string, null and non-class objects will be serialized and deserialized in a non-lossy manner.

Properties

Property Description
isEmpty: boolean Read-Only

Indicates whether the state is empty.

Methods

Get the value corresponding to a key. Return defaultValue if key is not found.

      
      get<T>(key: StateKey<T>, defaultValue: T): T
    
Parameters
key StateKey<T>
defaultValue T
Returns

T

Set the value corresponding to a key.

      
      set<T>(key: StateKey<T>, value: T): void
    
Parameters
key StateKey<T>
value T
Returns

void

Remove a key from the store.

      
      remove<T>(key: StateKey<T>): void
    
Parameters
key StateKey<T>
Returns

void

Test whether a key exists in the store.

      
      hasKey<T>(key: StateKey<T>): boolean
    
Parameters
key StateKey<T>
Returns

boolean

Register a callback to provide the value for a key when toJson is called.

      
      onSerialize<T>(key: StateKey<T>, callback: () => T): void
    
Parameters
key StateKey<T>
callback () => T
Returns

void

Serialize the current state of the store to JSON.

      
      toJson(): string
    
Parameters

There are no parameters.

Returns

string