Skip to content

Commit bdf2ed0

Browse files
author
majid noureddine
committed
switch on or off all appareils
1 parent f5321d8 commit bdf2ed0

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

src/app/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ <h2>Mes appareils</h2>
1111
<button class="btn btn-success"
1212
[disabled]="!isAuth"
1313
(click)="onAllumer()">Tout allumer</button>
14+
<button class="btn btn-danger"
15+
[disabled]="!isAuth"
16+
(click)="onEteindre()">Tout éteindre</button>
1417
</div>
1518
</div>
1619
</div>

src/app/app.component.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component } from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import { AppareilService } from './services/appareil.service';
33

44
@Component({
55
selector: 'app-root',
66
templateUrl: './app.component.html',
77
styleUrls: ['./app.component.scss']
88
})
9-
export class AppComponent {
9+
export class AppComponent implements OnInit {
1010

1111
isAuth = false;
1212
lastUpdate = new Promise((resolve, reject) => {
@@ -17,20 +17,7 @@ export class AppComponent {
1717
}, 2000
1818
);
1919
});
20-
appareils = [
21-
{
22-
name: 'Machine à laver',
23-
status: 'éteint'
24-
},
25-
{
26-
name: 'Frigo',
27-
status: 'allumé'
28-
},
29-
{
30-
name: 'Ordinateur',
31-
status: 'éteint'
32-
}
33-
];
20+
appareils: any[];
3421

3522
constructor(private appareilService: AppareilService) {
3623
setTimeout(
@@ -40,7 +27,19 @@ export class AppComponent {
4027
);
4128
}
4229

30+
ngOnInit() {
31+
this.appareils = this.appareilService.appareils;
32+
}
33+
4334
onAllumer() {
44-
console.log('On allume tout !');
35+
this.appareilService.switchOnAll();
36+
}
37+
38+
onEteindre() {
39+
if (confirm('Etes-vous sûr de vouloir éteindre tous vos appareils ?')) {
40+
this.appareilService.switchOffAll();
41+
} else {
42+
return null;
43+
}
4544
}
4645
}

src/app/services/appareil.service.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
export class AppareilService {
2+
appareils = [
3+
{
4+
name: 'Machine à laver',
5+
status: 'éteint'
6+
},
7+
{
8+
name: 'Frigo',
9+
status: 'allumé'
10+
},
11+
{
12+
name: 'Ordinateur',
13+
status: 'éteint'
14+
}
15+
];
16+
17+
switchOnAll() {
18+
for (let appareil of this.appareils) {
19+
appareil.status = 'allumé';
20+
}
21+
}
22+
23+
switchOffAll() {
24+
for (let appareil of this.appareils) {
25+
appareil.status = 'éteint';
26+
}
27+
}
228
}

0 commit comments

Comments
 (0)