Ali
Ali

Reputation: 2591

access to checkbox in typescript

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?

Upvotes: 0

Views: 681

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

Your code should be

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

and then

addNewBrand(createForm: NgForm) {
   console.log(createForm.value.active)
}

Upvotes: 1

Related Questions