File tree Expand file tree Collapse file tree 6 files changed +85
-24
lines changed Expand file tree Collapse file tree 6 files changed +85
-24
lines changed Original file line number Diff line number Diff line change 4
4
< h2 > Mes appareils</ h2 >
5
5
< p > Mis à jour le : {{ lastUpdate | async | date: 'd MMMM y' | uppercase }}</ p >
6
6
< ul class ="list-group ">
7
- < app-appareil *ngFor ="let appareil of appareils "
7
+ < app-appareil *ngFor ="let appareil of appareils; let i = index "
8
8
[appareilName] ="appareil.name "
9
- [appareilStatus] ="appareil.status "> </ app-appareil >
9
+ [appareilStatus] ="appareil.status "
10
+ [index] ="i "> </ app-appareil >
10
11
</ ul >
11
12
< button class ="btn btn-success "
12
13
[disabled] ="!isAuth "
13
14
(click) ="onAllumer() "> Tout allumer</ button >
15
+ < button class ="btn btn-danger "
16
+ [disabled] ="!isAuth "
17
+ (click) ="onEteindre() "> Tout éteindre</ button >
14
18
</ div >
15
19
</ div >
16
20
</ 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
+ import { AppareilService } from './services/appareil.service' ;
2
3
3
4
@Component ( {
4
5
selector : 'app-root' ,
5
6
templateUrl : './app.component.html' ,
6
7
styleUrls : [ './app.component.scss' ]
7
8
} )
8
- export class AppComponent {
9
+ export class AppComponent implements OnInit {
9
10
10
11
isAuth = false ;
11
12
lastUpdate = new Promise ( ( resolve , reject ) => {
@@ -16,30 +17,29 @@ export class AppComponent {
16
17
} , 2000
17
18
) ;
18
19
} ) ;
19
- appareils = [
20
- {
21
- name : 'Machine à laver' ,
22
- status : 'éteint'
23
- } ,
24
- {
25
- name : 'Frigo' ,
26
- status : 'allumé'
27
- } ,
28
- {
29
- name : 'Ordinateur' ,
30
- status : 'éteint'
31
- }
32
- ] ;
20
+ appareils : any [ ] ;
33
21
34
- constructor ( ) {
22
+ constructor ( private appareilService : AppareilService ) {
35
23
setTimeout (
36
24
( ) => {
37
25
this . isAuth = true ;
38
26
} , 4000
39
27
) ;
40
28
}
41
29
30
+ ngOnInit ( ) {
31
+ this . appareils = this . appareilService . appareils ;
32
+ }
33
+
42
34
onAllumer ( ) {
43
- 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
+ }
44
44
}
45
45
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { NgModule } from '@angular/core';
3
3
import { AppComponent } from './app.component' ;
4
4
import { AppareilComponent } from './appareil/appareil.component' ;
5
5
import { FormsModule } from '@angular/forms' ;
6
+ import { AppareilService } from './services/appareil.service' ;
6
7
7
8
@NgModule ( {
8
9
declarations : [
@@ -13,7 +14,9 @@ import { FormsModule } from '@angular/forms';
13
14
BrowserModule ,
14
15
FormsModule
15
16
] ,
16
- providers : [ ] ,
17
+ providers : [
18
+ AppareilService
19
+ ] ,
17
20
bootstrap : [ AppComponent ]
18
21
} )
19
22
export class AppModule { }
Original file line number Diff line number Diff line change 1
1
< li [ngClass] ="{'list-group-item': true,
2
2
'list-group-item-success': appareilStatus === 'allumé',
3
3
'list-group-item-danger': appareilStatus === 'éteint'} ">
4
- < div style ="width:20px;height:20px;background-color:red; "
5
- *ngIf ="appareilStatus === 'éteint' "> </ div >
4
+
6
5
< h4 [ngStyle] ="{color: getColor()} "> Appareil : {{ appareilName }} -- Statut : {{ appareilStatus }}</ h4 >
7
6
< 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
+
8
15
</ li >
Original file line number Diff line number Diff line change 1
1
import { Component , Input , OnInit } from '@angular/core' ;
2
+ import { AppareilService } from '../services/appareil.service' ;
2
3
3
4
@Component ( {
4
5
selector : 'app-appareil' ,
@@ -10,8 +11,10 @@ export class AppareilComponent implements OnInit {
10
11
11
12
@Input ( ) appareilName : string ;
12
13
@Input ( ) appareilStatus : string ;
14
+ @Input ( ) index : number ;
13
15
14
- constructor ( ) { }
16
+ constructor ( private appareilService : AppareilService ) {
17
+ }
15
18
16
19
ngOnInit ( ) {
17
20
}
@@ -23,4 +26,12 @@ export class AppareilComponent implements OnInit {
23
26
return 'red' ;
24
27
}
25
28
}
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
+ }
26
37
}
Original file line number Diff line number Diff line change
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
+ }
28
+
29
+ switchOnOne ( i : number ) {
30
+ this . appareils [ i ] . status = 'allumé' ;
31
+ }
32
+
33
+ switchOffOne ( i : number ) {
34
+ this . appareils [ i ] . status = 'éteint' ;
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments