18

I am using Angular 2 Typescript. I am facing a problem wherein I need to submit a form which contains check boxes. I need values that are in the attributes of checkboxes. The checkboxes are dynamic, so any number of checkboxes will be there.

 <div class="checkbox" *ngFor="#label of labelList">
      <div class="col-sm-4">
           <label><input type="checkbox" value="{{label.Id}}">{{ label.Name }}</label>
      </div>   
 </div>

6 Answers 6

21

I think this should work (not tested)

<div class="checkbox" *ngFor="let label of labelList">
  <div class="col-sm-4">
    <label>
      <input type="checkbox" value="{{label.Id}}" (change)="checkboxes[$event.target.getAttribute('value')]=$event.target.checked">
        {{ label.Name }}</label>
  </div>   
</div>

and store the values of changed checkboxes in checkboxes in your component.

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

5 Comments

And what will be on typescript (angular2) side. what will be the function ?
No function, just an object checkboxes that contains the values of the changed checkboxes. If you provide more information about what you actually try to accomplish I might be able to make more suggestions.
console the value of checkbox ?
Sorry, don't understand your comment? console.debug(this.checkboxes)?
What about you providing more information about your requirements first?
2

I use this for the checkboxes: ng2-material checkbox

And you could do this in your component:

<md-checkbox [checked]="exists(label.Id)" (click)="toggle(label.Id)"></md-checkbox>

  selected = [];
  @Output() selectedChange:EventEmitter<any> = new EventEmitter();

  toggle(id) {
    var index = this.selected.indexOf(id);
    if (index === -1) this.selected.push(id);
    else this.selected.splice(index, 1);
    this.selectedChange.emit(this.selected);
  }

  exists(id) {
    return this.selected.indexOf(id) > -1;
  }

Comments

2

Input

<input type="checkbox" (change)="selectionChange($event.srcElement, dataItem)">

Change Event

selectionChange(input: HTMLInputElement) {
    input.checked === true
      ? doSomethingIfTrue()
      : doSomethingIfFalse();
}

Comments

0

You have to specify name attribute for checkbox to able to track it on backend.

Comments

0
enter code here

submitForm(form:NgForm){
console.log(form.value);
}
<div class="checkbox" *ngFor="#label of labelList">
       <div class="col-sm-4">
            <label><input type="checkbox" name='label{{label.Id}}'  value="{{label.Id}}">{{ label.Name }} </label>
       </div>   
 </div>

Use name attribute including label.id,so you will get all value in array form.

GUESS THIS WILL HELP.

Comments

-4

I think you want to know checkbox that checked or not which you can easily get in the form of boolean value by specifying ng-model attribute for input tag attribute.

<md-checkbox ng-model="ctrl.someBooleanValue"></md-checkbox>

for more info check angular material md-checkbox and angular material md-checkbox demo

1 Comment

The question asks for help with Angular 2; "ng-model" is Angular 1 syntax.

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.