[英]Why checkbox event status returns string “on” instead of true/false?
I have a custom checkbox as child component which is being added in a parent component. 我有一个自定义复选框作为子组件,正在添加到父组件中。 I pass the
ngModel
, name
etc. correctly and try to update the model
with the status true/false
based on checkbox status using EventEmitter
. 我正确地传递了
ngModel
, name
等,并尝试使用EventEmitter
根据复选框状态使用状态为true/false
来更新model
。
Unfortunately the status I get is "on"
as a string
instead of boolean
不幸的是,我得到的状态是
"on"
为string
而不是boolean
Via Chrome console, I can track the status and the event
. 通过Chrome控制台,我可以跟踪状态和
event
。 It works correct and puts out the expected result. 它工作正确,并给出了预期的结果。 Just the
model
and two way binding gets a string value and in my case it's "on"
, and it keeps this string even if I uncheck the checkbox. 只是
model
和双向绑定获得一个字符串值,在我的情况下,它为"on"
,即使我取消选中该复选框,它也保留该字符串。 in other words there is even no "off"
换句话说,甚至没有
"off"
child.component.html: child.component.html:
<input type="checkbox"
name="{{passCheckBoxName}}"
#ngForm="ngModel"
[ngModel]="model"
(ngModelChange)="onChange($event)"
required>
child.component.ts : child.component.ts :
@Input() model: boolean;
@Output() modelChange: EventEmitter<any> = new EventEmitter();
onChange(event) {
console.log('this.model: ' + ${this.model});
this.model = event;
// event.checked doesn't work for me. output then is undefined
// this.model = event.checked;
console.log(event);
this.modelChange.emit( event );
// event.checked doesn't work for me. output then is undefined
// this.modelChange.emit( event.checked );
}
parent.component.html : parent.component.html :
<child-checkbox [parentFormGroup]="form"
[name]="'nameOfCheckbox'"
[ngModel]="name""
ngDefaultControl>
</child-checkbox>
I have done some code for you: https://stackblitz.com/edit/angular-odkm2n?file=src%2Fapp%2Fhello.html the model value is boolean here 1. Why you create the child component only with checkbox? 我已经为您完成了一些代码: https : //stackblitz.com/edit/angular-odkm2n?file= src%2Fapp%2Fhello.html模型值在此处为布尔值1.为什么仅使用复选框创建子组件? 2. Why you use Input() with "model" when you event don't take it into the child component?
2.为什么在事件未将Input()带入子组件时将其与“模型”一起使用? (also for the Output())
(也用于Output())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.