I have search input for the book title and I need it to find books without reloading the page. My code works at the moment witht he reloading of the page, I type book name and push submit.
How can I rewrite this code in order to use observable, thus search for the books without reloading the page?
filterByName(){
this.filteredItems = [];
if(this.inputName != ""){
this.books.forEach(element => {
if(element.author.toUpperCase().indexOf(this.inputName.toUpperCase())>=0 ||
element.title.toUpperCase().indexOf(this.inputName.toUpperCase())>=0){
this.filteredItems.push(element);
}
});
}else{
this.filteredItems = this.books;
}
console.log(this.filteredItems);
this.init();
}
HTML:
<div class="form-group">
<label>Filter </label>
<input type="text" id="inputName" [(ngModel)]="inputName"/>
<input type="button" (click)="filterByName()" value="Apply"/>
</div>