我正在使用Angular4,我不知道如何声明对象集合,然后添加项目和删除项目? 感谢您的帮助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);
https://stackoverflow.com/questions/12870291/typescript-typed-array-usage谷歌是你最好的朋友 –
在什么情况下... http get,* ngFor,other ? – JGFMK
请指定更详细的“对象集合”。对象是什么样的?你想用这个系列做什么? –