0

I have a form in html like this:

<form (ngSubmit)="addNewBrand(f)" #f="ngForm">
    <label>name:</label>
    <input type="text" name="name" required ngModel #name="ngModel">

    <label>active:</label>
    <input type="checkbox" name="active" required ngModel #active="ngModel">

    <button type="submit" [disabled]="!f.valid">Save</button>

</form>

in ts file:

addNewBrand(form:NgForm){
    console.log(form.value.name + ' -  ' + form.value.active);
}

but I can't access to checkbox in ts file. What should I do?

5
  • What is the problem exactly? I tried it and it works, the 'save' button is disabled when active is not checked. Commented Jun 3, 2018 at 7:10
  • I want to get the value of checkbox, but I can't. Commented Jun 3, 2018 at 7:11
  • Is my method incorrect? Commented Jun 3, 2018 at 7:12
  • So, you wanna get a true/false value of the checkbox. However, when checkbox not checked, the save button is disabled. Why don't you remove the [disabled]="!f.valid" binding? Commented Jun 3, 2018 at 7:22
  • Nooo, my question is, how can I access a checkbox item that defined in an html form, in typescript file? Commented Jun 3, 2018 at 7:26

1 Answer 1

1

Your code should be

 <input type="checkbox" [ngModel]="active" name="Active"> Active

and then

addNewBrand(createForm: NgForm) {
   console.log(createForm.value.active)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Sajeetharan

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.