0

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.

Unfortunately the status I get is "on" as a string instead of boolean

Via Chrome console, I can track the status and the 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. in other words there is even no "off"

child.component.html:

<input type="checkbox"
        name="{{passCheckBoxName}}" 
        #ngForm="ngModel"
        [ngModel]="model" 
        (ngModelChange)="onChange($event)"
        required>

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:

<child-checkbox [parentFormGroup]="form"
                [name]="'nameOfCheckbox'"
                [ngModel]="name"" 
                ngDefaultControl>
</child-checkbox>
7
  • Please provide a MCVE using e.g. Stackblitz. I've created a simpler version of this that works as expected (with true and false). Commented Jul 4, 2018 at 16:51
  • Sorry, If I understand you, you have created a demo for that on Stackbliz which works as it should be. Returns true/false. I just can see a sample for an angular app start/basic. Commented Jul 4, 2018 at 17:04
  • No - I'm suggesting you create a demo of your issue on Stackblitz as I am unable to reproduce your problem myself. Commented Jul 4, 2018 at 17:05
  • Ok, I'll try. Never have build a sample on Sblitz. Commented Jul 4, 2018 at 17:07
  • It doesn't have to be Stackblitz - You can create a Github repo or something else. We just need something that's complete and shows the problem. Commented Jul 4, 2018 at 17:07

1 Answer 1

1

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? 2. Why you use Input() with "model" when you event don't take it into the child component? (also for the Output())

Sign up to request clarification or add additional context in comments.

14 Comments

I don't see what you're talking about at all. Following the link, I get an empty app from your StackBlitz link. Sorry.
I see now what you mean. But the issue is about a checkbox and event with boolean status true/false which should be passed to the model.
To your question regarding creating the separated component for just one checkbox, I would like to be flexible adding multiple checkboxes in the parent component and make the label, name, placeholderetc. configurable as much as possible. And therefore having a custom form. I do also have custom input and buttons.
Could you tell me, are you emmiting true/false value when checbox checked?
you check the checkbox, value of the checkbox set into variable model and then we can emmit it to parent component with this.modelChange.emit(model)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.