Skip to content

Allow signals sync between component and a service #60243

@tomer953

Description

@tomer953

Which @angular/* package(s) are relevant/related to the feature request?

No response

Description

Problem

Sometimes we want to sync signals from components to a service (or everywhere else I Assume)

@Component()
export class SomeComponent {
  count = input(0)
  double = computed(() => this.count() * 2)
}
@Injectable()
export class SomeService {
  // Imagine I need the count/double here as well
}

Current solutions

  1. using effect() to sync between the component and the service
  effect(() => this.someService.double.set(this.double())

Obviously not the purpose of effects

  1. redesign the data-flow so the signal source will be on higher level

this is not always easy to implement, and in some cases even impossible, since we not always control everything

Proposed solution

Allow some new kind of utility to "Proxy" or "Sync" signals

class SomeService {
 // will be updated from the component
 count = syncedSignal<number>();
}

class SomeComponent {
 someService = inject(SomeService)

 count = input(0)
 double = computed(() => this.count() * 2)

 constructor() {
  // "move" the signal to a service
  syncSignal(this.count, this.someService.count) 
 }
}
  • probably be one-way only
  • worth exploring connect from ngxtension

Alternatives considered

.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions