1

I am new angular , in my angular 4 project I have a JSON array by which I am making a list of checkboxes. I am unable to submit value of these dynamic checkbox.

My json array is:-

    let chekboxArray = 
[
    {
        section:'Programming Skills',
        choices:[
            {
                name:'C'
                value:'c'
            },

            {
                name:'C++'
                value:'c++'
            },
            {
                name:'Java'
                value:'java'
            }
        ]   
    },
    {
        section:'Interest',
        choices:[
            {
                name:'Movies'
                value:'movies'
            },

            {
                name:'Tv Series'
                value:'tvseries'
            },
            {
                name:'Football'
                value:'football'
            }
        ]   
    }
]

My HTML is:-

 <form #customChoiceform="ngForm" (ngSubmit)="addToCart(customChoiceform);">
    <div *ngFor="let single of chekboxArray">
        <label>{{ single.section }}</label>
        <div *ngFor="let choice of single.choices;let i=index">
            <label>{{ choice.name }}</label>
            <input type="checkbox" [value]="{{ choice.value }}" name="{{ 'choices'+i }}" />
        </div>
    </div>
    <div>
        <button type="submit" />Submit</button>
    </div>
 </form>

In my component.ts file:

addToCart(form:NgForm){
  console.log(form);
}

Please Help How to manage this kind of thing.That will be helpful for my project.

1
  • you don't have a form closing tag? Commented May 24, 2018 at 6:32

1 Answer 1

0

Have you added FormsModule reference in your module?

import { FormsModule } from '@angular/forms'; 
@NgModule({
    imports: [
        FormsModule,
        HttpModule,   
         ]
    })

To submit form you need a formsModule.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.