Search code examples
angulareventsangular5keyup

Angular 5 keyup error


I am getting an error that says

ERROR TypeError: _co.onKey is not a function at Object.eval [as handleEvent]

import { Component } from "@angular/core";


@Component({
    selector: "sandbox",
    templateUrl: "sandbox.component.html",
    styleUrls: ["./sandbox.component.css"]
})

export class SandboxComponent {
    fireEvent(e){
        console.log(e.type);
    }
}

html

<input (keyup)="fireEvent($event)">

Solution

  • looks like you just have to add type="name" to your input tag.

    TS :

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular 5';
          fireEvent(e){
            console.log(e,e.keyCode);
        }
    }
    

    HTML :

      <input type="name" (keyup)="fireEvent($event)">
    

    Sample here : https://stackblitz.com/edit/angular-jbym58?file=app%2Fapp.component.ts