Correction Ex1 Angular
Correction Ex1 Angular
//constructeur
constructor(id:number, title:string, categories?: string[], image?:
string){
this.id = id; this.categories =categories;
this.image=image;this.title=title;
}
}
Product.component.ts
@Component({
selector: 'app-product',
standalone: true,
imports: [NgFor, NgIf, FormsModule],
templateUrl: './product.component.html',
styleUrl: './product.component.css'
})
export class ProductComponent implements OnInit{
ngOnInit()
{
this.product = new Product(1,'Iphone 9', ['smartphone',
'electronic'],'https://cdn.dummyjson.com/product-images/1/thumbnail.jpg');
if(this.color==undefined)
this.color= 'white';
}
Page | 1
augmenter()
{this.product.id ++;}
}
Product.component.html
<h1>Gestion du produit</h1>
<table border="1" aign="center" [style.background-color]="color">
<tr>
<th>id</th><td>{{product.id}}</td></tr>
<tr><th>title</th><td>{{product.title}}</td></tr>
<tr> <th>image</th><td><img [src]="product.image"></td></tr>
<tr>
<th>Categories</th>
<td >
<p *ngIf="product.categories == undefined; else elseBlock">Liste
vide</p>
<ng-template #elseBlock>
<ul>
<li *ngFor="let c of product.categories">
{{c}}
</li>
</ul>
</ng-template>
</td>
</tr>
<tr>
<td></td><td>
<button (click)="augmenter()">Incrementer id</button><br><br>
</td>
</tr>
<tr>
<th>
Modifier color :
</th> <td>
<input type="text" [(ngModel)]=" color">
</td>
</tr>
</table>
App.component.html
<app-product color="red"></app-product>
<app-product color="green"></app-product>
<app-product></app-product>
Page | 2