简体   繁体   English

为什么复选框事件状态返回字符串“ on”而不是true / false?

[英]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 . 我正确地传递了ngModelname等,并尝试使用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.

相关问题 为什么切换复选框返回 false? - Why toggle checkbox returns false? 为什么复选框以角度显示真假 - why checkbox showing true and false in angular Angular 7,没有NgModel时如何取复选框状态(是/否)? - Angular 7, without NgModel how to take the checkbox status (true / false)? 更改复选框使用“Y”和“N”而不是 false 和 true - Change checkbox use 'Y' and 'N' instead of false and true 使用反应形式将复选框绑定到值而不是真/假 - Bind checkbox to value instead of true/false with reactive forms 复选框“ Y” /“ N”的自定义值,而不是true / false。 角度2 - Custom Value of Checkbox 'Y'/'N' instead of true/false. Angular 2 为什么我的 ngModal 值得到一个字符串值,而不是我的 html select 中的 boolean 值 true 或 false? - Why is my ngModal value getting a string value instead of the boolean value true or false in my html select? Angular - 始终选中字符串值“true”或“false”的复选框 - Angular - checkbox for string value of "true" or "false" gets checked always 如何在0/1中获取复选框的值而不是true / false反应形式angular4 - How to get value of checkbox in 0/1 instead of true/false reactive forms angular4 isTokenExpired 方法返回 true 和 false - isTokenExpired method returns true and false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM