R
R
Roman Rakzin2020-06-08 19:31:14
Angular
Roman Rakzin, 2020-06-08 19:31:14

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;
  }

}

markup

<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>

connection

<app-dropbox-comp [array]="store['sortCarsParameters']['data']"  
 [selected]="store['sortCarsParameters']['selected']"></app-dropbox-comp>

Question:
1) If I leave the component, I can't "close" it
2) The selected value stays selected inside the component, but doesn't go up as a double bind and [selected]="store['sortCarsParameters']['selected' ]" stays the same. How to change this value?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question