Angular add and delete data

Angular add and delete data

<div class="search_box">
    <input type="text" placeholder="请输入……"  [(ngModel)]="keyWord"><button (click)="searchBtn()" >搜索</button>
<hr>
    <div class="search_list" *ngFor="let item of historyList;let key=index">
        {{item}} <button (click)="delete(key)">X</button>
    </div>
</div>

export class ToDolistComponent implements OnInit {
  public keyWord: string;
  public historyList: any[] = [];
  constructor() { }

  ngOnInit() {
  }
  searchBtn() {
    if (this.historyList.indexOf(this.keyWord) == -1) {
      this.historyList.push(this.keyWord);
    }
    this.keyWord = '';
  }
  delete(key) {
    this.historyList.splice(key, 1);
  }
}

Published 24 original articles · won praise 4 · Views 4462

Guess you like

Origin blog.csdn.net/Amo__/article/details/101270693