2017-06-08 53 views
0

在keyup事件觸發時的搜索輸入字段中,顯示keyup事件後數據再次觸發的數據點擊事件正在工作,但是我希望一旦keyup或click被觸發,不行。防止當keyup事件發生時在angular2中的默認點擊事件

這是我在header.html中

<form> 
    <div class="box"> 
     <div class="container-1"> 
      <input type="search" id="search" placeholder="Type 
       your search..." name="searchStr" (keyup.enter)="searchKey(input.value)" 
       [(ngModel)]="searchStr" #input> 
      <a (click)="searchKey(input.value)"><img 
       src="/assets/search.png" class="search-img"></a> 
     </div> 
    </div> 
</form> 

這裏是我的形式header.component.ts

searchKey(value: any) { 
    console.log(value); 
    this.router.navigate(['search', { searchkey : value }]); 
} 

預先感謝您。

回答

0

嘗試設置一個標誌變量,它會檢查是否搜索或不喜歡,

export ... { 

    flag = true; 

    searchKey(value: any) { 
     console.log(value); 
     this.flag && this.router.navigate(['search', { searchkey : value }]); 
     this.flag=false; 
    } 

    // don't forget to add a condition when flag must be set to true. 
} 

希望這會幫助你。