gpt4 book ai didi

angular - 模糊事件未在 Angular 分量上触发

转载 作者:太空狗 更新时间:2023-10-29 17:53:19 28 4
gpt4 key购买 nike

我正在尝试获得一种 react 形式来正确处理模糊事件,但它似乎无法对模糊做出正确 react 。

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

@Component({
selector: 'app-combo-box-basic',
templateUrl: './combo-box-basic.html'
})
export class ComboBoxBasic {

@HostListener('blur', ['$event.target']) onBlur(target) {
console.log(`onBlur(): ${new Date()} - ${JSON.stringify(target)}`);
}
}
<div class="btn-group mr-3">
<input (blur)="onBlur($event.target)" type="text" class="btn btn-outline-success">
<div class="btn-group" ngbDropdown role="group" aria-label="Button group with nested dropdown">
<button (blur)="onBlur($event.target)" class="btn btn-outline-primary" ngbDropdownToggle></button>
<div class="dropdown-menu" ngbDropdownMenu>
<button class="dropdown-item">One</button>
<button class="dropdown-item">Two</button>
<button class="dropdown-item">Four!</button>
</div>
</div>
</div>

最佳答案

经过大量搜索和试验后,我发现了一些需要让它正常工作的项目。 A fully functional demo can be found here

  1. 外部元素必须具有:tabindex="0" 以便它可以聚焦。这是通过以下方式实现的:@HostBinding('attr.tabindex') tabindex = '0';

  2. 为模糊事件添加一个 HostListener 将捕获外部组件:@HostListener('blur', ['$event.target']) onBlur(target) { ... }

  3. 您仍然需要捕获内部组件的模糊事件,以便点击进入和退出仍会触发。 (blur)="onBlur($event.target)"

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

@Component({
selector: 'app-combo-box-basic',
templateUrl: './combo-box-basic.html'
})
export class ComboBoxBasic {

// Make sure container can receive focus or else blur events won't be seen.
@HostBinding('attr.tabindex') tabindex = '0';
@HostListener('blur', ['$event.target']) onBlur(target) {
console.log(`onBlur(): ${new Date()} - ${JSON.stringify(target)}`);
}
}
<div class="btn-group mr-3">
<input (blur)="onBlur($event.target)" type="text" class="btn btn-outline-success">
<div class="btn-group" ngbDropdown role="group" aria-label="Button group with nested dropdown">
<button (blur)="onBlur($event.target)" class="btn btn-outline-primary" ngbDropdownToggle></button>
<div class="dropdown-menu" ngbDropdownMenu>
<button class="dropdown-item">One</button>
<button class="dropdown-item">Two</button>
<button class="dropdown-item">Four!</button>
</div>
</div>
</div>

关于angular - 模糊事件未在 Angular 分量上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52177292/

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