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 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 "
1314 (click) ="onAllumer() "> Tout allumer</ button >
15+ < button class ="btn btn-danger "
16+ [disabled] ="!isAuth "
17+ (click) ="onEteindre() "> Tout éteindre</ button >
1418 </ div >
1519 </ div >
1620</ 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' ;
23
34@Component ( {
45 selector : 'app-root' ,
56 templateUrl : './app.component.html' ,
67 styleUrls : [ './app.component.scss' ]
78} )
8- export class AppComponent {
9+ export class AppComponent implements OnInit {
910
1011 isAuth = false ;
1112 lastUpdate = new Promise ( ( resolve , reject ) => {
@@ -16,30 +17,29 @@ export class AppComponent {
1617 } , 2000
1718 ) ;
1819 } ) ;
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 [ ] ;
3321
34- constructor ( ) {
22+ constructor ( private appareilService : AppareilService ) {
3523 setTimeout (
3624 ( ) => {
3725 this . isAuth = true ;
3826 } , 4000
3927 ) ;
4028 }
4129
30+ ngOnInit ( ) {
31+ this . appareils = this . appareilService . appareils ;
32+ }
33+
4234 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+ }
4444 }
4545}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { NgModule } from '@angular/core';
33import { AppComponent } from './app.component' ;
44import { AppareilComponent } from './appareil/appareil.component' ;
55import { FormsModule } from '@angular/forms' ;
6+ import { AppareilService } from './services/appareil.service' ;
67
78@NgModule ( {
89 declarations : [
@@ -13,7 +14,9 @@ import { FormsModule } from '@angular/forms';
1314 BrowserModule ,
1415 FormsModule
1516 ] ,
16- providers : [ ] ,
17+ providers : [
18+ AppareilService
19+ ] ,
1720 bootstrap : [ AppComponent ]
1821} )
1922export class AppModule { }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11import { 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}
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