css Toggle switch does not change checkbox value/state

瘦欲@ 提交于 2019-12-13 09:11:41

问题


I am trying to create an toggle a switch. the tempate I used is from:

https://www.w3schools.com/howto/howto_css_switch.asp

every things works fine but the checked or value of the (hidden) checkbox does not change. I need this value for my angular(4) binding

How can you acchieve this?


回答1:


you can do this using binding.

<input type="checkbox" [checked]="checkbx"
(change)="checkbx = !checkbx" formControlName="name">

In Ts file:
private checkbx: boolean = true;

constructor(private fb: FormBuilder){
    this.heroForm = this.fb.group({
    name: [this.checkbx, Validators.required ],
    });
}

addvalue(checkbx: boolean){
  console.log(this.heroForm.value)
}

check this live link



来源:https://stackoverflow.com/questions/49027915/css-toggle-switch-does-not-change-checkbox-value-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!