File tree Expand file tree Collapse file tree 3 files changed +45
-17
lines changed Expand file tree Collapse file tree 3 files changed +45
-17
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ <h2>Mes appareils</h2>
11
11
< button class ="btn btn-success "
12
12
[disabled] ="!isAuth "
13
13
(click) ="onAllumer() "> Tout allumer</ button >
14
+ < button class ="btn btn-danger "
15
+ [disabled] ="!isAuth "
16
+ (click) ="onEteindre() "> Tout éteindre</ button >
14
17
</ div >
15
18
</ div >
16
19
</ div >
Original file line number Diff line number Diff line change 1
- import { Component } from '@angular/core' ;
1
+ import { Component , OnInit } from '@angular/core' ;
2
2
import { AppareilService } from './services/appareil.service' ;
3
3
4
4
@Component ( {
5
5
selector : 'app-root' ,
6
6
templateUrl : './app.component.html' ,
7
7
styleUrls : [ './app.component.scss' ]
8
8
} )
9
- export class AppComponent {
9
+ export class AppComponent implements OnInit {
10
10
11
11
isAuth = false ;
12
12
lastUpdate = new Promise ( ( resolve , reject ) => {
@@ -17,20 +17,7 @@ export class AppComponent {
17
17
} , 2000
18
18
) ;
19
19
} ) ;
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 [ ] ;
34
21
35
22
constructor ( private appareilService : AppareilService ) {
36
23
setTimeout (
@@ -40,7 +27,19 @@ export class AppComponent {
40
27
) ;
41
28
}
42
29
30
+ ngOnInit ( ) {
31
+ this . appareils = this . appareilService . appareils ;
32
+ }
33
+
43
34
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
+ }
45
44
}
46
45
}
Original file line number Diff line number Diff line change 1
1
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
+ }
2
28
}
You can’t perform that action at this time.
0 commit comments