Professional Documents
Culture Documents
TP Angular 2 Cor
TP Angular 2 Cor
Solutionnaire
Exercice 2 :
Créer un service qui permet d’afficher une liste de produit en cliquant sur un bouton.
Sol :
productID:number ;
name: string ;
price:number;
public getProducts() {
let products:Product[];
products=[
new Product(1,'Memory Card',500),
new Product(1,'Pen Drive',750),
new Product(1,'Power Bank',100)
]
return products;
}
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
products:Product[];|undefined ;
productService;
constructor(){
this.productService=new ProductService();
}
getProducts() {
this.products=this.productService.getProducts();
}
Contenu de app.component.html
Atelier de développement 2Ginfo
<div class="container">
<h1 class="heading"><strong>Services </strong>Demo</h1>
<div class='table-responsive'>
<table class='table'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of products;">
<td>{{product.productID}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
</tr>
</tbody>
</table>
</div>
</div>