gpt4 book ai didi

html - 检测 Angular 组件外部的点击

转载 作者:技术小花猫 更新时间:2023-10-29 11:31:18 24 4
gpt4 key购买 nike

如何检测 Angular 组件外部的点击?

最佳答案

import { Component, ElementRef, HostListener, Input } from '@angular/core';

@Component({
selector: 'selector',
template: `
<div>
{{text}}
</div>
`
})
export class AnotherComponent {
public text: String;

@HostListener('document:click', ['$event'])
clickout(event) {
if(this.eRef.nativeElement.contains(event.target)) {
this.text = "clicked inside";
} else {
this.text = "clicked outside";
}
}

constructor(private eRef: ElementRef) {
this.text = 'no clicks yet';
}
}

A working example - click here

关于html - 检测 Angular 组件外部的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com