首页 > 解决方案 > 如何防止按钮形成Angular?

问题描述

我有一个搜索表格:

<form class="navbar-form">
    <div class="input-group no-border">
        <input type="text" #term class="form-control" (keyup.enter)="search()" placeholder="поиск..." />
        <button class="btn btn-white btn-round btn-just-icon" (click)="search()">
            <i class="material-icons">search</i>
            <div class="ripple-container"></div>
        </button>
    </div>
</form>

它应该按clickand搜索key.enter

search(): void {
   const value = this.term.nativeElement.value ? this.term.nativeElement.value.toString().trim() : null;
   this.router.navigate(['search?=' + value]);

问题是当我单击按钮时它会重新加载页面。我也不喜欢条件const value = this.term.nativeElement.value ? this.term.nativeElement.value.toString().trim() : null;

我该如何改进它?

我试图通过$eventsearch($event)

search(e) {
   e.eventPrevent();
}

它对我不起作用。

标签: angular

解决方案


它应该是:

e.preventDefault();

推荐阅读