我想删除一个学生对象我在我父组件一个学生数组。学生被通过将他们的名字的方法删除。然而,认为没有更新。下面是使用splice
我对删除方法的代码
deleteStudent(name: string) {
for (var x = 0; x < this.students.length; x++) {
if (this.students[x].name === name) {
this.students.splice(x, 1);
this.students = [...this.students];
console.log(x);
return;
}
}}
下面是父类的完整代码:
import { Component, OnInit } from "@angular/core";
import { student } from "../../student";
@Component({
selector: "app-students",
templateUrl: "./students.component.html",
styleUrls: ["./students.component.css"]
})
export class StudentsComponent implements OnInit {
students: student[] = [
{ name: "Marwa", level: "undergrad" },
{ name: "Heba", level: "undergrad" },
{ name: "Amal", level: "postgrad" }
];
txt = "";
toBeDeleted: student;
public undergradsList = this.undergrads();
constructor() {}
ngOnInit() {
this.undergrads();
console.log(this.undergrads);
}
undergrads() {
var Arrayres = new Array();
for (var i = 0; i < this.students.length; i++) {
if (this.students[i].level === "undergrad") {
Arrayres.push(this.students[i].name);
}
}
console.log(Arrayres);
return Arrayres;
}
deleteStudent(name: string) {
for (var x = 0; x < this.students.length; x++) {
if (this.students[x].name === name) {
this.students.splice(x, 1);
this.students = [...this.students];
this.undergradsList = this.undergrads();
console.log(x);
return;
}
}
// const studentIndex = this.students.findIndex(e => e.name === name);
console.log("hi delte");
}
}
下面是父母的HTML组件的代码:
<h1>{{ txt }}</h1>
<app-under-grads
(childEvent)="txt = $event"
[studentList]="undergradsList"
(studentDeletion)="deleteStudent($event)"
>
</app-under-grads>
这里是我的孩子组件的代码:
import { Component, OnInit, Input, EventEmitter, Output } from "@angular/core";
import { student } from "../../student";
@Component({
selector: "app-under-grads",
templateUrl: "./under-grads.component.html",
styleUrls: ["./under-grads.component.css"]
})
export class UnderGradsComponent implements OnInit {
@Input() public studentList;
@Output() public childEvent = new EventEmitter();
constructor() {}
ngOnInit() {
console.log(this.studentList);
}
sendInfo() {
this.childEvent.emit("Hello I am sending this to the parent");
}
@Output() public studentDeletion = new EventEmitter<string>();
delete(name: string) {
// console.log("delete it");
this.studentDeletion.emit(name);
console.log(name);
}
}
这里是正在屏幕上呈现我的子组件的HTML。 studentList是名称的字符串的数组
<h2>UnderGrads</h2>
<div>
<ul>
<li *ngFor="let x of studentList">
{{ x }} <button (click)="delete(x)">X</button>
</li>
</ul>
</div>
我认为undergradsList
相同students
因此,在您deleteStudent功能
它应该是这样的
deleteStudent(name: string) {
for (var x = 0; x < this.students.length; x++) {
if (this.students[x].name === name) {
this.students.splice(x, 1);
this.undergradsList = [...this.students];
console.log(x);
return;
}
}}
你突变的数组,然后分配在上面的新数组。突变是坏M'kay!
我使用的过滤器,这些样的删除
this.students = this.students.filter(s => student !== s);
你是完整的学生对象与名称。这里是一个固定StackBlitz。
https://stackblitz.com/edit/angular-3nhmkw?file=src/app/app.component.ts
这是不是与你的组件代码的问题。你需要更新你在哪里遍历数组的HTML。
只需使用trackBy
与ngFor。
<tr *ngFor="let hero of heroes; trackBy: trackHero" >
<td>{{hero.name}}</td>
</tr>
trackHero(index, hero) {
console.log(hero);
return hero ? hero.id : undefined;
}
在trackHero,你需要返回这是你的对象唯一的属性。