Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Can focusin and focusout be in one event?

Can focusin and focusout be in one event? What is it called, then? If not, is there a way to merge this in one function?

hide(e:any) {
  $('.suggestion').hide();
}
show(e:any) {
  $('.suggestion').show();
}
<section class="search-bar-wrapper" (focusout)="hide($event)" (focusin)="show($event)">
like image 591
Char Avatar asked Jun 23 '17 05:06

Char


People also ask

What is the difference between Blur and Focusout?

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .

What is Focusout event?

The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.

What is Focus event in angular?

The ng-focus directive tells AngularJS what to do when an HTML element gets focus. The ng-focus directive from AngularJS will not override the element's original onfocus event, both will be executed.

What is an event in angular?

In Angular 8, event binding is used to handle the events raised by the user actions like button click, mouse movement, keystrokes, etc. When the DOM event happens at an element(e.g. click, keydown, keyup), it calls the specified method in the particular component.


2 Answers

First thing is you need to add tabindex attribute to section to make it to get focus event. Otherwise, it won't get focus event.

Focus event get triggered when an element is focus-able. Every time you click the element it is always get focused and we can remove the focus only on click outside the element. So, we can't remove focus on click event of the same element.

focus and focusout both are different events we can't merge them

You can use *ngIf also

<section 
    class="search-bar-wrapper" 
    tabindex="-1" 
    (focus)="show($event)" 
    (focusout)="hide($event)"
>

<div class="suggestion" *ngIf="canSee">
    This is a suggestion
</div>

in the class of the component

casSee: boolean = false;

show(e: any) {
   this.canSee = true;
}

hide(e: any) {
   this.canSee = false;
}
like image 152
Mr_Perfect Avatar answered Oct 22 '22 11:10

Mr_Perfect


You can use (focus) and (focusout) in Angular 2/4.

like image 6
Deepak swain Avatar answered Oct 22 '22 13:10

Deepak swain