Listen to focus event in child component – handler doesn’t capture focus event

I’m using Angular 16 – a parent component consumes a child component – the parent component is as follows

parent.component.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><app-gotoitem #initialFocusComponent (initialFocusNativeElementFocus)="focusHandler($event)"></app-gotostudent>
</code>
<code><app-gotoitem #initialFocusComponent (initialFocusNativeElementFocus)="focusHandler($event)"></app-gotostudent> </code>
<app-gotoitem #initialFocusComponent (initialFocusNativeElementFocus)="focusHandler($event)"></app-gotostudent>

parent.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@ViewChildren('initialFocusComponent') initialFocusComponents: QueryList<any>;
...
focusHandler(e) {
console.log("do something");
}
...
this.initialFocusComponents.first.setFocus();
</code>
<code>@ViewChildren('initialFocusComponent') initialFocusComponents: QueryList<any>; ... focusHandler(e) { console.log("do something"); } ... this.initialFocusComponents.first.setFocus(); </code>
@ViewChildren('initialFocusComponent') initialFocusComponents: QueryList<any>;
...
focusHandler(e) {
  console.log("do something");
}
...
this.initialFocusComponents.first.setFocus();

In my child component (gotoitem)

child.component.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Output() initialFocusNativeElementFocus = new EventEmitter();
@ViewChild('initialFocus') initialFocus: ElementRef;
...
fromEvent(this.initialFocus.nativeElement, 'focus') //(B)
.pipe(take(1))
.subscribe(elem => {
this.initialFocusNativeElementFocus.emit(); //(C) <--this doesn't get executed
});
...
public setFocus(): void {
this.initialFocus.nativeElement.focus(); //(A)
}
</code>
<code>@Output() initialFocusNativeElementFocus = new EventEmitter(); @ViewChild('initialFocus') initialFocus: ElementRef; ... fromEvent(this.initialFocus.nativeElement, 'focus') //(B) .pipe(take(1)) .subscribe(elem => { this.initialFocusNativeElementFocus.emit(); //(C) <--this doesn't get executed }); ... public setFocus(): void { this.initialFocus.nativeElement.focus(); //(A) } </code>
@Output() initialFocusNativeElementFocus = new EventEmitter();
@ViewChild('initialFocus') initialFocus: ElementRef;
...
fromEvent(this.initialFocus.nativeElement, 'focus') //(B)
    .pipe(take(1))
    .subscribe(elem => {
    this.initialFocusNativeElementFocus.emit(); //(C) <--this doesn't get executed
});
...
public setFocus(): void {
  this.initialFocus.nativeElement.focus(); //(A)
}

However after the child executes the “.focus()” statement (A), the ‘focus’ event listener (B) doesn’t capture the ‘focus’ event – and hence the ’emit()’ (C) is never executed.

I can see the focus event handler attached to the desired HTML element (which this.initialFocus.nativeElement points to, which is a kendo-dropdownlist) – however the focus event listener has two events attached (the first refers to the “document” element [setup via zone.js], the second refers to the element pointed to by this.initialFocus.nativeElement) – see below

If I manually delete the “document” event handler and focus on the element in question the ‘focus’ handler captures the event and the ’emit()’ is executed

How is this scenario normally handled – regardless of whether the “document” event handler is there or not I need the focus event I have defined to also run – or do I need to remove the “document” event handler – if so how ?

Many thanks

2

As far as I can see kendo-dropdownlist can not receive focus (and should NOT by its design). Enter in dev-tools console:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>setInterval(() => console.log(document.activeElement), 1000);
</code>
<code>setInterval(() => console.log(document.activeElement), 1000); </code>
setInterval(() => console.log(document.activeElement), 1000);

Then focus desired element and u will see that span element is focused.

1

I resolved the issue by listening to the focusin event rather than the focus event. The problem was because the focus event doesn’t bubble whereas the focusin does

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật