1

I'm trying to use mat-checkboxes as input in my form, but can't really find anything on docs regarding it.

Html

<section class="checkbox-section">
 <mat-checkbox [(ngModel)]="bChecked">Blogs</mat-checkbox>
 <mat-checkbox [(ngModel)]="wChecked">Web</mat-checkbox>
 <mat-checkbox [(ngModel)]="oChecked">Online News</mat-checkbox>
 </section>

Typescript

    public form = {
    name: null,
    email: null,
    birthday: null,
    company: null,
    interests: [],
    newsletter: null,
    address: null,
    password: null,
    password_confirmation: null,
  };

I'm trying to get the values of the checkboxes which are static and then push to my interests array

1
  • If value is static then you can do without bind checkbox with ngModel , You can use click event and based on that just pass value to your function. Commented Oct 17, 2018 at 9:29

3 Answers 3

1

Change your HTML as below.

<section class="checkbox-section">
  <mat-checkbox [(ngModel)]="bChecked" (change)="onCheckboxChagen($event, 'Blogs')">Blogs</mat-checkbox>
  <mat-checkbox [(ngModel)]="wChecked" (change)="onCheckboxChagen($event, 'Web')">Web</mat-checkbox>
  <mat-checkbox [(ngModel)]="oChecked" (change)="onCheckboxChagen($event, 'Online News')">Online News</mat-checkbox>
  </section>

As you need to use static label for checkbox value, you can pass a static label as the second argument to the onCheckboxChagen method. According to the value of the checkbox you can push and pop elements from the interests array.

TS changes should be as below.

  onCheckboxChagen(event, value) {

    if (event.checked) {

      this.interests.push(value);
    } 
    if (!event.checked) {

      let index = this.interests.indexOf(value);

      if (index > -1) {

        this.interests.splice(index, 1);
      }
    }
  }

Working Demo

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

1 Comment

Thanks this is more similar to what i was looking for
1

I don't know if this might suit your requirement, but from what I understand all you need to do is change your code to something like this to get an info on which of the checkboxes are actually checked.

Typescript

public form = {
    ...
    interests: {
      blogs: false,
      web: false,
      onlineNews: false,
    },
    ...
  };

HTML

<section class="checkbox-section">
    <mat-checkbox [(ngModel)]="form.interests.blogs">Blogs</mat-checkbox>
    <mat-checkbox [(ngModel)]="form.interests.web">Web</mat-checkbox>
    <mat-checkbox [(ngModel)]="form.interests.onlineNews">Online News</mat-checkbox>
 </section>

I don't know how pushing the value true or false to any array makes any sense or give information on which input is checked.

Stackblitz demo

1 Comment

Thank you! this taught me some new techniques for checking matboxes
0

You can add a change event on each of your mat-checkbox and call the following method to fullify your array:

(The below example could be upgraded using a single method with another parameter to know which checkbox had a change event)

<section class="checkbox-section">
 <mat-checkbox [(ngModel)]="bChecked" (change)="assignValues1($event)">Blogs</mat-checkbox>
 <mat-checkbox [(ngModel)]="wChecked" (change)="assignValues2($event)">Web</mat-checkbox>
 <mat-checkbox [(ngModel)]="oChecked" (change)="assignValues3($event)">Online News</mat-checkbox>
 </section>

In my example to access the new boolean value you can do like this :

assingValues1(event: MatCheckboxChange) {
var newVal = event.checked;
// your code here
}

1 Comment

It will return true/false, not the value

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.