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' });
- 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)
I feel like this is unnecessary complication - adding too many options to just simple thing and mixing responsibilities. Potential
hostBindingwhich I provided above would still becomputed-like so this should work:@devversion has a function in the constructor been considered?
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:
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
@HostBindingwith signals is not necessarily something we would want to support now (without more evidence that there are requiring use-cases). Mostly because:hostsand@HostBindingachieve the same thing.@HostBinding, as Alex pointed out.@HostBinding) with signal inputs (that are no longer decorator-based) feels confusing.hostfield of e.g.@Directiveis 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:I would love to see this option:
@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
and it is working right now
I think this solution is the best
For some cases also we can get a reference for results of hostBinding method.
For other solutions, this also sounds good
@maurotamm it’s totally supported, you just need to use the correct syntax.
See https://angular.dev/guide/components/host-elements#binding-to-the-host-element
https://ngxtension.netlify.app/utilities/components/host-binding/
@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:
I’m curious about potential disadvantages of such approach if you already discussed it. For me it would make a great fit with
effectinstead of having one thing in constructor and another in component’s metadata but I suppose this can be something that you just plan to tackleThe 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.
Because
barin this context is not defined.this.baris 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
barin the scope the decorator.My initial thought is that
@HostBindingis expecting raw values. ASignalis not supported, even without signal inputs. In thehostfield of the component the signal can be read and used to e.g. bindattr.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
This rule is still valid? https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin/docs/rules/no-host-metadata-property.md
Why is the syntax
instead of
which would make more sense to me. In other words: why is the value a string? Just wondering.