Skip to content

feat(core): shorthand for exposing model input/output pairs in hostDirectives #70748

Description

@mauriziocescon

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

core

Description

Hey!

Currently, exposing a model through hostDirectives requires listing its input and output separately:

@Directive({ selector: '[dir]' })
export class Dir {
  readonly value = model.required<number>();
}

@Component({
  selector: 'Comp',
  hostDirectives: [
    {
      directive: Dir,
      inputs: ['value: aliasedValue'],
      outputs: ['valueChange: aliasedValueChange'],
    },
  ],
  template: `
    <button (click)="value.set(value() - 1)">-</button>
    <span>{{ value() }}</span>
    <button (click)="value.set(value() + 1)">+</button>
  `,
})
export class Comp {
  protected readonly value = inject(Dir).value;
}

@Component({
  imports: [Comp],
  template: `
    <h3>Value: {{ v() }}</h3>
    <Comp [(aliasedValue)]="v" />
  `,
})
export class App {
  protected readonly v = signal(0);
}

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

The hostDirectives API predates model(), so this makes sense. However, since createComponent now supports twoWayBinding(), I think it would be nice to offer a similar convenience in hostDirectives and reduce this boilerplate (assuming details such as overlapping declarations can be addressed). In general, a concrete use case is aria/directives.

So something like this:

hostDirectives: [
    { directive: Dir, models: ['value: aliasedValue'] },
]

Thanks a lot!

Cheers

Activity

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: coreIssues related to the framework runtimecore: host directivesfeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions