Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - @HostListener('focus') doesn't work

Tags:

angular

I'm trying to catch a focus event by @HostListener. But it doesn't work well for me.

I saw an article below.

HTML5 event handling(onfocus and onfocusout) using angular 2

Also saw a plunker appeared on the article.

http://plnkr.co/edit/0K1nxX2ltZ4ztfe5uJ6E?p=preview

It worked on me. However I changed it as below, it wouldn't work then.

@Component({
    selector: 'my-app',
    template: `<input name="date" type="text" (focus)="focusFunction()" (focusout)="focusOutFunction()">` 
})
export class App {
    @HostListener('focus', ['$event.target'])
    onFocus(target: any) 
        console.log("Focus called from HostListener");
        target.type = 'date';
    }
    @HostListener('focusout', ['$event.target'])
    onFocusout(target: any) {
        console.log("Focus out called from HostListener");
        target.type = 'text';
    }

    focusOutFunction(){
        console.log("Focus out called");
    }
    focusFunction(){
        console.log("Focus called");
    }
}

Regarding focusout, both of them are called. But focus (focusin) works only focusFunction, not work onFocus by @HostListener.

How can I make @HostListener works with focus event?

like image 736
Yusuke Masuda Avatar asked Feb 03 '17 21:02

Yusuke Masuda


People also ask

How HostListener works in Angular?

HostListener - Declares a host listener. Angular will invoke the decorated method when the host element emits the specified event. @HostListener - will listen to the event emitted by the host element that's declared with @HostListener . HostBinding - Declares a host property binding.

Can we use HostListener in component?

Introduction. @HostBinding and @HostListener are two decorators in Angular that can be really useful in custom directives. @HostBinding lets you set properties on the element or component that hosts the directive, and @HostListener lets you listen for events on the host element or component.

What is Focus event in angular?

In this article, we are going to see what is focus event in Angular 10 and how to use it. The (focus) event is triggered when an element gains its focus. Syntax: <input (focus)='functionName()'/>

What is HostBinding in Angular?

HostBindinglinkDecorator that marks a DOM property as a host-binding property and supplies configuration metadata. Angular automatically checks host property bindings during change detection, and if a binding changes it updates the host element of the directive.


2 Answers

use blur instead of focusout HostListener

@HostListener('blur') onblur() { ....... ....... }

like image 109
Rinold Avatar answered Oct 09 '22 04:10

Rinold


That's because @HostListener attaches a listener to the host element. In this case your host element is the <my-app></my-app> element. When you focus inside the <input> element the focus event does not bubble up to its parent. Also, only certain elements can actually fire a focus event and the my-app element is not one of them.

The example you posted uses a @Directive which they place on the <input> element. Which obviously makes it possible to listen for focus events on the directive.

You can however have a custom element fire a (focus) event by setting the tabindex="0" as attribute.

like image 32
Poul Kruijt Avatar answered Oct 09 '22 04:10

Poul Kruijt