1

I am working on autocomplete application

<form novalidate [formGroup] ="formG">
  <input type="text" placeholder="enter"
         formGroupName="formCont" class="searchBox"
         (click)="showDrop()" id="search"> 
  <input type="text">
</form>
<div class="seracDropDown" *ngIf = "showDropDown"></div>

and the code part is:

import { Component, HostListener } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
selector : "app-root",
  templateUrl : './app.component.html',
  styleUrls : ['./app.component.css']
})

export class AppComponent {

showDropDown : boolean = false;
formG = new FormGroup({
  formCont : new FormControl()
})

showDrop (){
  this.showDropDown = !this.showDropDown;
}
@HostListener('click',['$ev.target'])
onClickCalled(target) {
  if(target.id =="search") {
    console.log("S");
  }
}

But it throws error:

Cannot read property 'target' of undefined

Could anyone help me?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
sona mondal
  • 69
  • 1
  • 6
  • Possible duplicate of [Javascript "cannot read property "bar" of undefined](https://stackoverflow.com/questions/8004617/javascript-cannot-read-property-bar-of-undefined) – Capricorn Jul 13 '18 at 19:31

1 Answers1

0

Just change $ev.target to $event.target

@HostListener('click',["$event.target"]) onClickCalled(target){
    if(target.id =="search")
    {
      console.log("S");
    }
 }
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91