在 Angular 中,如何区分程序化点击和用户点击?

In Angular, how can I tell between a programmatic and user click?

假设我有一个简单的

<li (click)="clicked($event)">click me</li>

然后我如何在函数中判断事件是程序触发的还是用户触发的?

可以用 jQuery 收集,但我没有机会只用 js 来收集。

您可以检查 event.isTrusted 属性 以确定 click 事件是如何生成的:

The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.

clicked(e: Event) {
  console.log(e.isTrusted ? "Clicked by user" : "Clicked programmatically");
}

有关演示,请参阅 this stackblitz