angular: Combination of `HostBinding` and signals is not supported (input signals too)

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

core

Description

I am currentely trying to use new input signal api but I am unable to replace this code with DRY.

  @HostBinding('attr.title')
  @Input({ required: true, alias: 'appModalClose' })
  public closeBtnText!: string;

Proposed solution

Maybe it should be replaced by something like

public closeBtnText = input.required<string>({ alias: 'appModalClose', hostBinding: ['attr.title'] });

Alternatives considered

I was trying to find some solutions, but this is not working

@HostBinding('attr.title')
public closeBtnText = input.required<string>({ alias: 'appModalClose' });
  1. Host is stacked in memory leak
@Component({
  // eslint-disable-next-line @angular-eslint/no-host-metadata-property
  host: {
    '[title]': 'closeBtnText()',
  },
})
export class ModalCloseComponent {
  public closeBtnText = input.required<string>({ alias: 'appModalClose' });
}

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 52
  • Comments: 33 (11 by maintainers)

Most upvoted comments

I would love to see this option:

public closeBtnText = input.required<string>({ alias: 'appModalClose', hostBinding: ['attr.title'] });

I feel like this is unnecessary complication - adding too many options to just simple thing and mixing responsibilities. Potential hostBinding which I provided above would still be computed-like so this should work:


closeBtnText = input.required<string>({ alias: 'appModalClose' });

constructor() {
  hostBinding('attr.title', () => this.closeBtnText());
}

@devversion has a function in the constructor been considered?

constructor() {
  hostBinding('class.hidden', () => this.isHidden());
  hostBinding('class.hidden', this.isHidden); // alternative: isHidden is a signal
}

Oh, so we’re stuck with using a string expression with no IDE support? That’s unfortunate. Any plans to support Signal in a HostBinding?

It’s also possible to split the two, and write:

appModalClose = input.required<string>();

@HostBinding('attr.title')
get title() { return this.appModalClose(); }

If this is going to be the recommended approach going forward, should this style guide rule be updated?

https://angular.io/guide/styleguide#style-06-03

Thanks again for this issue. I’ve chatted a little with @pkozlowski-opensource and we both felt like supporting @HostBinding with signals is not necessarily something we would want to support now (without more evidence that there are requiring use-cases). Mostly because:

  • We likely want to avoid encouraging two patterns. hosts and @HostBinding achieve the same thing.
  • There are still ways to use @HostBinding, as Alex pointed out.
  • Combining a decorator (@HostBinding) with signal inputs (that are no longer decorator-based) feels confusing.
  • Calling signals directly in the host field of e.g. @Directive is a more natural/obvious consumption in the reactive context. I.e. it matches with the mental model that “reading signals” is necessary so that the directive/component will update whenever those change.

You mentioned something with memory leak when using host. Can you share more details please? Below seems to work as expected:

@Directive({
  standalone: true,
  selector: 'my-dirx',
  host: { '[attr.title]': 'bla()' },
})
export class MyDir {
  bla = input.required<string>();
}

I would love to see this option:

public closeBtnText = input.required<string>({ alias: 'appModalClose', hostBinding: ['attr.title'] });

@dkimmich-onventis I use vscode. It would be nice if angular highlighted and syntax/error checked the host field in vscode. 😃

@devversion I have doubble checked

@Directive({
  standalone: true,
  selector: 'my-dirx',
  host: { '[attr.title]': 'bla()' },
})
export class MyDir {
  bla = input.required<string>();
}

and it is working right now

I think this solution is the best

@Component({/*...*/})
export class Component {
    readonly customInput = input(false);
    readonly customBinding = hostBinding('attr.custom', () => this.customInput());
}

For some cases also we can get a reference for results of hostBinding method.

For other solutions, this also sounds good

@k3nsei angular-eslint/angular-eslint#1772 I’ve suggested the change, thank you.

@maurotamm it’s totally supported, you just need to use the correct syntax.

@Component({
  host: {
    '[style.--background-color]': 'backgroundColor()',
  },
})
export class MyComponent {
  backgroundColor = signal('red');
}

See https://angular.dev/guide/components/host-elements#binding-to-the-host-element

@alxhub just wondering - is no-decorator signal-way approach even considered or you just haven’t touched it yet? I’m about the way @c-harding provided:

constructor() {
  hostBinding('class.hidden', () => this.isHidden());
  hostBinding('class.hidden', this.isHidden); // alternative: isHidden is a signal
}

I’m curious about potential disadvantages of such approach if you already discussed it. For me it would make a great fit with effect instead of having one thing in constructor and another in component’s metadata but I suppose this can be something that you just plan to tackle

The style guide will be updated as part of the upcoming v18 release. This is tracked by ##54284.

@cyraid Which IDE do you use? I am using WebStorm, and it works like a charm, with code highlighting, suggestions and everything.

image

Because bar in this context is not defined. this.bar is also not defined, as we are inside the decorator and not inside the class. It needs to be a string so TypeScript doesn’t throw errors.

There is no reference named bar in the scope the decorator.

My initial thought is that @HostBinding is expecting raw values. A Signal is not supported, even without signal inputs. In the host field of the component the signal can be read and used to e.g. bind attr.title. I will look more into this

@k3nsei angular-eslint/angular-eslint#1772 I’ve suggested the change, thank you.

Thanks for pointing it out but that was not my main point, merely a nudge in understanding the issue. Sorry if my message was misunderstood:

The problem I highlighted is that both the official Angular websites conflict on the recommendation of using host vs HostBinding/HostListener. That should be addressed on the angular repo 😃

@JeanMeche but HostBinding and HostListener are working with class inheritance. We are using that in our FormControl abstractions to not repeat code.That means regression in some way, because class decorators are not inherited.

We mostly need to do that, to not copy/paste code to use custom form controls inside angular material form field, as it’s requires a lot of stuff to be implemented in custom form control.

We could of course use some mix of hostDirectives and base abstract class which delegates methods and getters and setters calls to this directives basically works as a proxy. But this sounds like a hack to me.

@nickdobson Yeah, we likely need to discuss this more with the team. Good point.

This could be addressed by creating an ng-host element which we could bind to more intuitively. See https://github.com/angular/angular/issues/19119

Why is the syntax

host: {
  '[class.foo]': 'bar()', 
},

instead of

host: {
  '[class.foo]': bar(), 
},

which would make more sense to me. In other words: why is the value a string? Just wondering.