简体   繁体   中英

Listen to enter click on <input> component

I'm using Angular 4.2.2 with the ES6 style. Right now, I'm at a loss as how to get my code to run a function when the user hits the ENTER key while an field has focus.

Right now, my html looks like so.

  <div class="box" id="redbox" class="app-flex-item"><!-- flex item -->
    <input id="searchTermInput" class="search-input"  ng-keydown="runSearch($event)">
  </div>

and my component controller looks like so.

@Component({
    ...
})
export class AppComponent {

  runSearch(ev): void {
    console.log("Run Search Invoked");
    this.searchResultService.getHeroesSlowly().then(searchResponseResult => {
        this.searchResponseResult = searchResponseResult;
        this.searchResults = searchResponseResult.searchResults;
        this.showDivider = true;
      }
    )
  }
}

Unfortunately, whenever I hit the ENTER key, I don't even see "Run Search Invoked" outputted to the console.

Does anyone know how to listen to enter events on an input component?

Any help is greatly appreciated.

对于ENTER键,请使用keyup.enter

  <input id="searchTermInput" #box (keyup.enter)="runSearch(box.value)">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM