I am working with Angular4 and I don't know how to declare a collection of objects and then adding an item and removing an item ? Thanks for your help
Here is what did : The Component :
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"}];
}
The template :
<table>
<tr *ngFor="let personne of personnes">
<td>{{personne.id}}</td>
<td>{{personne.nom}}</td>
</tr>
</table>
It works fine, but now I would want to manage my collection of objects, for instance the user will provide datas and I'll create a new object with those datas and I'll add this new object to my collection. I tried this, but it doesn't work :
personnes: Object[] = [];
personne: any = {{id:1, nom: "Alain"};
this.personnes.push(this.personne);
or
personnes: any[] = [];
personne: any = {{id:1, nom: "Alain"};
this.personnes.push(this.personne);