Skip to content

Support for setting Input Signals programmatically given a directive instance #54782

Open

Description

@Fernker

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

core

Description

With Input signals they are read-only which forces any consumer of the component to have to set the Inputs through the template. For some situations with a parent/child relationship (such as projected content) the parent needs to be able to programmatically set values of the Inputs as they don't have access to set the template bindings.

As a component library author there are often cases where we allow something like an icon component to be projected. The icon component has an input that allows several sizes. When the icon is used in a specific component in a projection slot that component can query through a ContentChildren query. That component can further set the value of the @Input to "enforce" a given size. This "parent" component can't set the value through the template bindings as it doesn't have template access to projected content, just component class instances.

Here's a non-working example of where a parent component allows projected icon components. It is trying to query for icon content and then set the size input. We've been able to accomplish in the past with @Inputs and the getter/setter pattern. We'd love to be able to move to Input signals but this blocks us.

https://stackblitz.com/edit/stackblitz-starters-g5jzxx?file=src%2Fcomponents%2Fparent%2Fparent.component.ts

Proposed solution

Some way for consumers to set the value of an Input programmatically. I realize they are read-only but this breaks the notion of a component not knowing how it is consumed and used. Consumers are allowed to set Inputs through the template OR through the component class instance with a reference.

Alternatives considered

We can continue to use a getter/setter syntax to fake Input signals. Something like this:

@Input() get size(): number {
  return this._size;
}

set size(value: number) {
   this._size.set(value);
}
private _size = signal();

It would be nicer and easier to maintain if we can utilize Input signals programmatically.

Model Inputs are also a potential option

I don't feel that the Model inputs are the solution here as that would mean all of our Inputs would have to be Model inputs because as component library authors we don't know how a consumer will access our Inputs (via template or programmatically). Whereas Model inputs more feels like it's trying to solve ControlValueAccessor stuff and not necessarily meant for this.

Activity

  1. Harpush commented on Mar 8, 2024

    @Harpush

    One can say it is half way related to #47398
    As injecting the component ref of the projected component (in this case with content child) would allow setInput usage

  2. Fernker commented on Mar 8, 2024

    @Fernker
    Author

    @Harpush is the setInput available automatically when using @ContentChildren? I've only used it with dynamic components and still am not clear if it's currently available?

  3. Harpush commented on Mar 8, 2024

    @Harpush

    @Harpush is the setInput available automatically when using @ContentChildren? I've only used it with dynamic components and still am not clear if it's currently available?

    No it isn't. The issue I linked suggest adding it to injection of host component from directive. This issue seems half way similar as it can be solved if content child could get not the component instance but the component ref instead.

  4. Fernker commented on Mar 8, 2024

    @Fernker
    Author

    Although setInput seems pretty broadly typed, which might make sense with a ComponentRef but it would need to be narrower to be most useful.

  5. Harpush commented on Mar 8, 2024

    @Harpush

    Although setInput seems pretty broadly typed, which might make sense with a ComponentRef but it would need to be narrower to be most useful.

    There is an open issue for better setInput types too

  6. Fernker commented on Mar 8, 2024

    @Fernker
    Author

    Would the componentRef still include a reference to the component class to be able to access other methods on the class that aren't inputs?

  7. RobbyRabbitman commented on Mar 15, 2024

    @RobbyRabbitman

    I had a similar situation, which was solved by unwrapping the Signal Input with the SIGNAL symbol and use setSignalValueFn (or something like that... typing this comment from mobile). I don't know if that's intended, but it worked :D

  8. added
    area: core
    on Mar 27, 2024
  9. added this to the needsTriage milestone on Mar 27, 2024
  10. alxhub commented on Mar 28, 2024

    @alxhub
    Member

    @RobbyRabbitman just note that SIGNAL isn't public API.

  11. changed the title [-]Support for setting Input Signals programmatically for projected content children[/-] [+]Support for setting Input Signals programmatically given a directive instance[/+] on Sep 11, 2024
  12. thaoula commented on Dec 19, 2024

    @thaoula

    @alxhub could we get a method that does what @RobbyRabbitman in a supported way?

    We have directives that would like to update their host components. I have created a simplified example here:
    https://stackblitz.com/edit/stackblitz-starters-jxjmiswb?file=src%2Fmain.ts

    In real life the host component is something similar to NGSELECT with many inputs

    I have also commented on the issue regarding getting a reference to ComponentRef to utilise set input.
    #47398 (comment)

  13. DaSchTour commented on Dec 27, 2024

    @DaSchTour

    I have a somehow related issue while migrating to signal inputs. I have several directives that are used to programmatically set the inputs of another directive that is connected through hostDirectives and then injected into my directive.
    Or am I missing some undocumented API that makes it possibility to set the inputs of host directives programmatically?

  14. 60 remaining items

  15. hakimio commented on Nov 25, 2025

    @hakimio

    @splincode there is already a writable input(), it's called model(). The main issue here is that input() is not writable from directives and you can't do things like this.

  16. mauriziocescon commented on Nov 25, 2025

    @mauriziocescon

    Well, model is also doing the "right thing": in case, same value between parent and child. Which is never true for inputs, no matter what. As long as you own the entire stack of components, model is more or less what you need: see signal-forms.

    IMO the entire ticket is mostly related to components you don't own...

    ...especially knowing that it's not an issue at all in other modern frameworks.

    Regarding the other frameworks, I'm not aware of any one where inputs are writable (forbidden / discouraged / ...). All the others:

    • have hostless components (see Feature: Hostless components #18877),
    • they let you customise things by wrapping components,
    • when they have directives (attribute ones: vue / svelte), you can't set inputs,
    • in general, there is a clear split between the way you feed components (inputs / props) and the public interface (what you get with queries / injection)... something not available in angular and highly desirable IMHO.

    As usual ☮️

    @Directive({
      selector: '[setter]',
    })
    export class Setter {
      readonly child = inject(Child, { optional: true });
    
      private readonly domReady = afterNextRender(() => {
        this.child?.value.set('Setter');
      });
    }
    
    @Component({
      selector: 'Child',
      template: `
        <p>Child: {{ value() }}</p>`,
    })
    export class Child {
      // ✅ in sync with App.v1
      readonly value = model('Child');
    }
    
    @Component({
      selector: 'app-root',
      imports: [Setter, Child],
      template: `
        <h1>App: {{ v1() }}, {{ v2() }}</h1>
        <Child setter [(value)]="v1" />
        <hr />
        <Child [(value)]="v2" />
        <hr />
        <button (click)="update()">Update v1</button>`,
    })
    export class App {
      readonly v1 = signal('App');
      readonly v2 = signal('App');
    
      update() {
        this.v1.set(`${Date.now()}`);
      }
    }

    https://stackblitz.com/edit/stackblitz-starters-gvt2ylbo

  17. Ketec commented on Nov 25, 2025

    @Ketec

    Model has its own issues.

    I recall that the value did not reflect or trigger back when you used createComponent and setInput. Not sure if 21 fixed that.

    Linters also tend to suggest that model should be set readonly.

    Its likely only meant for two way binding through template (the banana in a box).

    It does not have transform support.
    Migration script auto creates input signal.
    So you are quickly going to have new lib releases that just went for input.

    Its not official either, so it can easily break in 22+ and again all the directives will fail.

    The longer this is left unsolved, the worse it all gets.

    People writing all kinds of custom hacks and workarounds.

    It should have been a drop in replacement like signals in templates were, without breaking millions of directives across codebases.

  18. Erbenos commented on Nov 25, 2025

    @Erbenos
    Contributor

    Thing is the old input setting never actually worked fully, but only by coencidence - either the properties were backed by getter/setter pairs, or they were never observed in any lifecycle hooks. So its not like the signal inputs broke anything - they just invalidated an approach that always depended on internal component implementation details, whether users were aware of it or not.

  19. mauriziocescon commented on Nov 25, 2025

    @mauriziocescon

    I recall that the value did not reflect or trigger back when you used createComponent and setInput. Not sure if 21 fixed that.

    Well, createComponent let you specify bindings now. And in a template, you can just bind to a model like an input (<Child setter [value]="v1()" />). So, only transform as far as I know (which - by the way - makes sense it's missing).

    I agree the doc could be more clear and the migrations can be misleading (but ok: IMHO it's not a drama either ).

        const comp = this.vcr().createComponent(TextInput, {
          directives: [
            {
              type: Highlight,
              bindings: [inputBinding('color', () => this.color())],
            },
          ],
          bindings: [
            inputBinding('label', this.label),
            twoWayBinding('value', this.value),
            outputBinding('btnClick', this.handleClick),
          ],
        });

    https://stackblitz.com/edit/stackblitz-starters-y2d9ac6d?file=src%2Fmain.ts

  20. splincode commented on Nov 26, 2025

    @splincode
    Contributor

    @splincode there is already a writable input(), it's called model(). The main issue here is that input() is not writable from directives and you can't do things like this.

    @hakimio sure, but what if I don’t need output event in component?

  21. hakimio commented on Nov 26, 2025

    @hakimio

    @hakimio sure, but what if I don’t need output event in component?

    I think it depends. If you are modifying the input inside your component, you usually do want to have the modified value reflected in a two-way-binding (there are exceptions, of course). On the other hand, if you are modifying some component (can also be UI library component) input from a separate directive or some other 3rd party context, you shouldn't be required to convert the input() to model() signal.

    Anyway, input() signal should just be a writable signal without having to resort to any special configs/flags or other hacks. It should be writable by default. The "race condition" contra-argument is a moot point - we have been updating Angular inputs since forever and nobody complained about it. input() signal should be fully backward compatible with @Input(). The community has expressed a clear need for this functionality and Angular framework devs (@alxhub) shouldn't just ignore it as if it's some minor issue nobody cares about.

  22. pburgmer commented on Nov 26, 2025

    @pburgmer
    Contributor

    Anyway, input() signal should just be a writable signal without having to resort to any special configs/flags or other hacks. It should be writable by default. The "race condition" contra-argument is a moot point - we have been updating Angular inputs since forever and nobody complained about it. input() signal should be fully backward compatible with @Input(). The community has expressed a clear need for this functionality and Angular framework devs (@alxhub) shouldn't just ignore it as if it's some minor issue nobody cares about.

    I don't agree on 'inputs should be writable'. You shouldn't change the value of in input from within the component. That's code smell and caused some serious trouble in our code base. We need to be able to change the input from the outside in some scenarios like directive. That's something completely different. It could be solved by having something like a DirectiveRef or ComponentRef which allows interaction with the component the same way Angular is dealing with it.

  23. MillerSvt commented on Nov 26, 2025

    @MillerSvt

    I agree. Inputs should not be writable. But since inputBindings already works in createComponent method, I can't find any reason not to do the same in components.

    I already posted (as I think) good solution for this problem:

    @Component({
        hostDirectives: [
            {
                directive: MyDirective,
                host: {
                    '[directiveInput]': 'componentValue()',
                },
            }
        ],
    })
    class MyCmp {
        componentValue = signal(...);
    }

    I think this solution can solve most of use cases in this thread.

  24. waterplea commented on Nov 26, 2025

    @waterplea
    Contributor

    @MillerSvt that's good UX and it would solve #50510 but this would absolutely not be enough.

  25. Ketec commented on Nov 26, 2025

    @Ketec

    For example:

    
    <kendo-multiselect myCustomDefaultsDirective [directiveInputValue]=[1] (componentOutput)="..."/>
    <kendo-multiselect myCustomDefaultsDirective [directiveInputValue]=[2] (componentOutput)="..."/>
    

    You want to set several defaults and utilities to standardize the usage for your product.
    There may be a 100 of these across the product.
    You still want to access the kendo-multiselect inputs and outputs. There is no way to automatically extend and proxy everything.

    This component hostDirective in a component wrapper does not help here.

  26. montella1507 commented on Dec 1, 2025

    @montella1507

    I wonder how is implemented that new [field] directive in Signal base forms...

    If you want to get "notifications" about control state, you can implement this interface:
    https://angular.dev/api/forms/signals/FormUiControl

    as you can see, there are mainly Inputs() and not Models..

    And field (or something above) has to be able to set those inputs...

    So it is Directive => Component input scenario.
    However i wasnt able to find the implementaiton how it is done.

  27. LoaderB0T commented on Oct 21, 2026

    @LoaderB0T

    Three concrete cases from a signal-only component library on v22 (jig), all currently solved with a SIGNAL hack:

    1. Directive -> component on the same element. A directive receives config from an ancestor and has to push it into an input of the component it sits on (inject(Comp, { self: true })). The template author never sees the value, so they can't bind it.
    2. Parent -> queried children. A container cascades a styling input to child components found via viewChildren. The children are also used standalone with their own binding, so neither model() nor a duplicate internal signal fits.
    3. Subclass -> inherited input. A subclass derives a base-class input from one of its own inputs.

    Core in v22 already does case 1, privately. ɵɵControlFeature gives Signal Forms' FormField a host whose setInputOnDirectives() writes inputs of the other directives on the same node via writeToDirectiveInput. That path applies transforms, calls ngOnChanges and resolves host-directive aliases, so it doesn't have the problems raised in #55082. The user-land hacks skip transforms and rely on private API.

    Could this be triaged? Exposing that same-element capability publicly would already cover most cases here.
    Reality is, it used to work with the old @input() syntax and doesn't work with signal inputs without workarounds. So why not make these workarounds official ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: inputs / outputscross-cutting: signals

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions