javascript - ลบรายการออกจากอาเรย์ที่เก็บไว้ใน Angular 2
ฉันต้องการลบรายการออกจากอาร์เรย์ที่เก็บไว้ใน Angular 2 โดยใช้ Type Script ฉันกำลังใช้บริการที่เรียกว่า Data Service โค้ดของ DataService:
export class DataService {
private data: string[] = [];
addData(msg: string) {
this.data.push(msg);
}
getData() {
return this.data;
}
deleteMsg(msg: string) {
delete [this.data.indexOf(msg)];
}
}
และคลาสคอมโพเนนต์ของฉัน:
import {Component} from '@angular/core'
import {LogService} from './log.service'
import {DataService} from './data.service'
@Component({
selector: 'tests',
template:
`
<div class="container">
<h2>Testing Component</h2>
<div class="row">
<input type="text" placeholder="log meassage" #logo>
<button class="btn btn-md btn-primary" (click)="logM(logo.value)">log</button>
<button class="btn btn-md btn-success" (click)="store(logo.value)">store</button>
<button class="btn btn-md btn-danger" (click)="send()">send</button>
<button class="btn btn-md " (click)="show()">Show Storage</button>
<button (click)="logarray()">log array</button>
</div>
<div class="col-xs-12">
<ul class="list-group">
<li *ngFor="let item of items" class="list-group-item" #ival>
{{item}}
<button class="pull-right btn btn-sm btn-warning" (click)="deleteItem(ival.value)">Delete
</button>
</li>
</ul>
</div>
<h3>{{value}}</h3>
<br>
</div>
`
})
export class TestsComponent {
items: string[] = [];
constructor(
private logService: LogService,
private dataService: DataService) {
}
logM(message: string) {
this.logService.WriteToLog(message);
}
store(message: string) {
this.dataService.addData(message);
}
send(message: string) {
}
show() {
this.items = this.dataService.getData();
}
deleteItem(message: string) {
this.dataService.deleteMsg(message);
}
logarray() {
this.logService.WriteToLog(this.items.toString());
}
}
ตอนนี้ ทุกอย่างใช้งานได้ดี ยกเว้นเมื่อฉันพยายามลบรายการ บันทึกแสดงว่ารายการยังอยู่ในอาเรย์ ดังนั้นจึงยังคงปรากฏบนหน้า ฉันจะเอารายการออกหลังจากเลือกมันด้วยปุ่มลบได้อย่างไร?
คำตอบ #1
คุณไม่สามารถใช้ delete เพื่อลบรายการออกจากอาเรย์ได้ สิ่งนี้ใช้เพียงเพื่อเอาคุณสมบัติออกจากอ็อบเจกต์เท่านั้น
คุณควรใช้ splice เพื่อลบสมาชิกออกจากอาร์เรย์:
deleteMsg(msg:string) {
const index: number = this.data.indexOf(msg);
if (index !== -1) {
this.data.splice(index, 1);
}
}
คำตอบ #2
ฉันคิดว่าวิธีของ Angular 2 ในการทำสิ่งนี้คือวิธี filter:
this.data = this.data.filter(item => item !== data_item);
โดยที่ data_item คือรายการที่ควรถูกลบ
คำตอบ #3
อย่าใช้ delete เพื่อลบรายการออกจากอาเรย์ ใช้ splice() แทน
this.data.splice(this.data.indexOf(msg), 1);
ดูคำถามที่คล้ายกัน: ฉันจะลบองค์ประกอบเฉพาะออกจากอาร์เรย์ใน JavaScript ได้อย่างไร?
ต้องสังเกตว่า TypeScript เป็นซูเปอร์เซตของ ES6 (อาร์เรย์เหมือนกันทั้งใน TypeScript และ JavaScript) ดังนั้นสามารถค้นหาโซลูชันของ JavaScript ได้แม้กำลังทำงานกับ TypeScript
คำตอบ #4
<tbody *ngFor="let emp of $emps;let i=index">
<button (click)="deleteEmployee(i)">Delete</button></td>
และ
deleteEmployee(i)
{
this.$emps.splice(i,1);
}
คำตอบ #5
คุณสามารถใช้แบบนี้ได้:
removeDepartment(name: string): void {
this.departments = this.departments.filter(item => item != name);
}
