The following code works (modulo any typos - the real code is much more complicated). The value of the expression appears in the warning when you click on the DOM element containing this directive.
@Directive({selector: '[alertOnClick]'})
export class AlertOnClick implements OnDestroy {
@Input('alertOnClick') clickFunction: (() => string);
@HostListener('click')
onClick() {
alert(this.clickFunction());
}
}
The problem is that it should be called as follows:
<button [alertOnClick]="() => String(new Date())">Time</button>
Edit: It seems that the Angular parser does not support anonymous functions. It must be called by naming the function on the controller.
I want to call it more naturally, something like this:
<button [alertOnClick]="String(new Date())">Time</button>
But if any such variation is displayed each time the button is pressed, the page will be displayed. I cannot have my life determine how to evaluate an expression at a time that I like, and not when the page is displayed. Any suggestions?
source
share