Angular 数据添加删除
<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);
}
}