Skip to content

Commit f211484

Browse files
author
majid noureddine
committed
switch on or off one appareill
1 parent bdf2ed0 commit f211484

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/app/app.component.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<h2>Mes appareils</h2>
55
<p>Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}</p>
66
<ul class="list-group">
7-
<app-appareil *ngFor="let appareil of appareils"
7+
<app-appareil *ngFor="let appareil of appareils; let i = index"
88
[appareilName]="appareil.name"
9-
[appareilStatus]="appareil.status"></app-appareil>
9+
[appareilStatus]="appareil.status"
10+
[index]="i"></app-appareil>
1011
</ul>
1112
<button class="btn btn-success"
1213
[disabled]="!isAuth"
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<li [ngClass]="{'list-group-item': true,
22
'list-group-item-success': appareilStatus === 'allumé',
33
'list-group-item-danger': appareilStatus === 'éteint'}">
4-
<div style="width:20px;height:20px;background-color:red;"
5-
*ngIf="appareilStatus === 'éteint'"></div>
4+
65
<h4 [ngStyle]="{color: getColor()}">Appareil : {{ appareilName }} -- Statut : {{ appareilStatus }}</h4>
76
<input type="text" class="form-control" [(ngModel)]="appareilName">
7+
8+
<button class="btn btn-sm btn-success"
9+
*ngIf="appareilStatus === 'éteint'"
10+
(click)="onSwitch()">Allumer</button>
11+
<button class="btn btn-sm btn-danger"
12+
*ngIf="appareilStatus === 'allumé'"
13+
(click)="onSwitch()">Eteindre</button>
14+
815
</li>

src/app/appareil/appareil.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, Input, OnInit } from '@angular/core';
2+
import { AppareilService } from '../services/appareil.service';
23

34
@Component({
45
selector: 'app-appareil',
@@ -10,8 +11,10 @@ export class AppareilComponent implements OnInit {
1011

1112
@Input() appareilName: string;
1213
@Input() appareilStatus: string;
14+
@Input() index: number;
1315

14-
constructor() { }
16+
constructor(private appareilService: AppareilService) {
17+
}
1518

1619
ngOnInit() {
1720
}
@@ -23,4 +26,12 @@ export class AppareilComponent implements OnInit {
2326
return 'red';
2427
}
2528
}
29+
30+
onSwitch() {
31+
if (this.appareilStatus === 'allumé') {
32+
this.appareilService.switchOffOne(this.index);
33+
} else if (this.appareilStatus === 'éteint') {
34+
this.appareilService.switchOnOne(this.index);
35+
}
36+
}
2637
}

src/app/services/appareil.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ export class AppareilService {
2525
appareil.status = 'éteint';
2626
}
2727
}
28+
29+
switchOnOne(i: number) {
30+
this.appareils[i].status = 'allumé';
31+
}
32+
33+
switchOffOne(i: number) {
34+
this.appareils[i].status = 'éteint';
35+
}
2836
}

0 commit comments

Comments
 (0)