Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HostBinding to anonymous lambda function?

I'm using @HostBinding to bind the results of a function to the host element's visible attribute:

@HostBinding('attr.visible')
private get visibleAttr(): any {
  return this._visible ? '' : null;
}

The function name seems redundant because nothing else will ever call this function. I tried to declare it like an anonymous lambda function but it didn't work:

@HostBinding('attr.visible') (() => {
  return this._visible ? '' : null;
})

Is there a way to declare this function anonymously, or at least with a lambda? Or does the Angular framework depend on it being named?

like image 718
Kyle V. Avatar asked Mar 15 '26 10:03

Kyle V.


1 Answers

@HostBinding is a decorator and needs a valid member to decorate. If your 2nd code example

(() => {
  return this._visible ? '' : null;
})

on it's own was a valid member it could work but this is not valid typescript (or javascript) for a member declaration. As such it can't work.

like image 126
Igor Avatar answered Mar 18 '26 00:03

Igor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!