Answer the question
In order to leave comments, you need to log in
How to catch the loss of focus from the component and change the value when selected not only in the component, but also in the binding?
There is an improved dropbox component and it displays data and then I want to pass the selected value up and hide the dropdown list when focus is lost. Here is the code
import { Component, Input, HostListener } from '@angular/core';
@Component({
selector: 'app-dropbox-comp',
templateUrl: './dropbox.component.html',
styleUrls: ['./dropbox.component.scss']
})
export class DropboxComponent{
@Input() selected: object;
@Input() array: [];
public isOpen = false;
constructor() { }
toggle(){
this.isOpen = !this.isOpen;
}
change_value(item){
this.selected = item;
}
@HostListener("blur") //Не работает
lostfocus(){
this.isOpen = false;
}
}
<div class="dropdown selectDropdown" [ngClass]="{'open': isOpen}" (click)="toggle()" (blur)="lostfocus()">
<ul (blur)="lostfocus()">
<li *ngFor="let item of array; let i = index" (click)="change_value(item)"><a>{{item.name}}</a></li>
</ul>
<span>{{selected['name']}}</span>
</div>
<app-dropbox-comp [array]="store['sortCarsParameters']['data']"
[selected]="store['sortCarsParameters']['selected']"></app-dropbox-comp>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question