【发布时间】:2017-12-31 13:22:28
【问题描述】:
我正在使用 Angular4,但我不知道如何声明一个对象集合,然后添加一个项目和删除一个项目? 感谢您的帮助
这是做了什么: 组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// tableaux d'objets
personnes: Object[]=[{id:1, nom: "Alain"},{id: 2, nom: "Clémentine"},{id: 3, nom: "Cathy"}];
}
模板:
<table>
<tr *ngFor="let personne of personnes">
<td>{{personne.id}}</td>
<td>{{personne.nom}}</td>
</tr>
</table>
它工作正常,但现在我想管理我的对象集合,例如用户将提供数据,我将使用这些数据创建一个新对象,并将这个新对象添加到我的集合中。 我试过了,但它不起作用:
personnes: Object[] = [];
personne: any = {{id:1, nom: "Alain"};
this.personnes.push(this.personne);
或
personnes: any[] = [];
personne: any = {{id:1, nom: "Alain"};
this.personnes.push(this.personne);
【问题讨论】:
-
stackoverflow.com/questions/12870291/… google 是你最好的朋友
-
在什么情况下... http get、*ngFor、其他?
-
请详细说明“对象的集合”。一个物体是什么样子的?你想用这个系列做什么?
-
您好,只用作任何数组,我建议在 (typescriptlang.org/docs/handbook/basic-types.html) 的官方网站中检查文档,这是一个使用您示例的测试 plnkr.co/edit/vsQWITwK48beVWVCiiAI?p=preview
-
非常感谢您的回答 :-)
标签: angular