如何在每次单击时使用角度为8的拼接从阵列中删除第一个对象



我有一个包含3个对象的数组。我希望每次单击都删除第一个对象。当我点击按钮时,只有第一次删除第一个对象,我需要每次从更新的数组结果中删除第一个物体。这是下面的代码https://stackblitz.com/edit/angular-nuwwva?file=src%2Fapp%2Fapp.component.html

app.component.ts

import { Component, OnInit } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular";
ngOnInit() {}
clickhere() {
const arraydata = [
{ name: "name1", value: 1 },
{ name: "name2", value: 2 },
{ name: "name3", value: 3 }
];
arraydata.splice(0, 1);
console.log(arraydata);
}
}

app.component.html

<button type="button" (click)="clickhere()">Click Me!</button>

这应该有效:

import { Component, OnInit } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular";
arraydata = [];
ngOnInit() {
this.arraydata = [{ name: "name1", value: 1 },
{ name: "name2", value: 2 },
{ name: "name3", value: 3 }
}];
clickhere() {
this.arraydata.splice(0, 1);
console.log(arraydata);
}
}

问候

最新更新