J'ai un commutateur On / Off dans mon projet Web:
HTML:
getSwitcherValue(onoffswitch) {
console.log("onoffswitch:"+onoffswitch.style.content);
}
CSS:
.onoffswitch-inner:before {
content: "ON";
padding-left: 10px;
background-color: #93297E; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 10px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
Je veux savoir si le mélangeur est activé ou désactivé, j'ai essayé d'obtenir la valeur, en utilisant le code suivant, mais cela ne fonctionne pas:
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span #onoffswitch class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
Avez-vous des idées comment obtenir la valeur du commutateur marche / arrêt?
5 Réponses :
utilisez onoffswitch.checked au lieu de onoffswitch.style.content
Remplacez par:
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" [(ngModel)]="isChecked">
<label class="onoffswitch-label" for="myonoffswitch">
<span #onoffswitch class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
Et ajoutez isChecked dans votre fichier ts en tant que booléen.
Vous utilisez Angular. Utilisez donc NgModel .
public onOff = false;
Et dans votre fichier TS :
<div class="onoffswitch">
<input type="checkbox" [(ngModel)]="onOff" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span #onoffswitch class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
Vous pouvez maintenant simplement cocher this.onOff pour voir si votre commutateur est "on" ou non.
p >
Si vous utilisez angular, utilisez simplement sa fonction appelée liaison bidirectionnelle
Réf: https://angular.io/api/forms/NgModel p>
pour la solution de votre problème:
Merci, votre solution m'a aidé
Vous pouvez essayer cette solution
Fichier HTML (par exemple app.component.html)
.onoffswitch-inner:before {
content: "ON";
padding-left: 10px;
background-color: #93297E; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 10px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
Ts File (app .component.ts)
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
isSwitched:boolean=true;
getSwitcherValue(onoffswitch) {
this.isSwitched=!this.isSwitched;
console.log("onoffswitch:"+this.isSwitched);
}
}
Fichier Css (app.component.css)
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
id="myonoffswitch" [checked]="isSwitched"
(change)="getSwitcherValue(isSwitched)">
<label class="onoffswitch-label" for="myonoffswitch">
<span #onoffswitch class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
Votre concept "onoffswitch" semble inapproprié. Je pense qu'il n'y a aucun moyen de lire la valeur du contenu par E.style.content. stackoverflow.com/ questions / 2651739 /…
Pourquoi voulez-vous obtenir une valeur de
cssqui n'est responsable que de l'apparence du bouton? Et avec quelle version Angular travaillez-vous?Je crois que c'est ce que vous voulez, stackoverflow. com / questions / 53930713 /… .. Vous devez utiliser l'entrée radio si vous utilisez un interrupteur à bascule.
@ HarunYılmaz Je veux vérifier si le commutateur est activé ou désactivé, angular2