Search code examples
angularsignals

Combine Input Signal with other signal


im implementing a custom form control with signals for the first time.

I want an input that can be used to disable the component. However since it also supports the setDisabledState method, I need to be able to set the disabledState somewhere.

This is my current approach.

  private disabled = input<boolean>(false);
  private disabledCall = signal<boolean>(false)
  public disable = computed(() => this.disabled() || this.disabledCall())

  setDisabledState(isDisabled: boolean): void {
    this.disabledCall.set(isDisabled)
  }

This feels terrible compared to the non-signal-based approach.

@Input()
public disabled = false;

setDisabledState(isDisabled: boolean): void {
   this.disabled = isDisabled
}

Is there anything I can do to improve the signal-based version?


Solution

  • 17.2 introduced Model Inputs, you could use the fact that they are writable input signals.

      private disabled = model<boolean>(false);
    
      setDisabledState(isDisabled: boolean): void {
        this.disabled.set(isDisabled)
      }