16

I have a list of checkboxes:

    <section *ngFor="let item of list">
      <mat-checkbox [checked]="item.value" (click)="toggle(_checkbox_value_here_)">{{item.key}}</mat-checkbox>
    </section>

I need to pass the checkbox value (_checkbox_value_here_) to my function, how would you do that?

I can see this related question How to get checkbox data in angular material but really is there a way go achieve that without using ngModel and reactive forms?

2
  • this value _value_ ? Commented Nov 13, 2018 at 13:11
  • @SergioEscudero just modified the question. Commented Nov 13, 2018 at 13:14

7 Answers 7

20

The click event on the checkbox is just the native click event, which doesn't know anything about the checkbox itself. Using the change event or just getting a handle on the MatCheckbox instance directly (e.g. with @ViewChildren) would be the recommended approach.

(c) https://github.com/angular/material2/issues/13156#issuecomment-427193381

<section *ngFor="let item of list">
  <mat-checkbox [checked]="item.value" (change)="toggle($event)">{{item.key}}</mat-checkbox>
</section>
Sign up to request clarification or add additional context in comments.

Comments

9

you may use the element's checked property.

<mat-checkbox #c (click)="toggle(!c.checked)">Check me!</mat-checkbox>
  • notice it's !c.checked, because by the time you click it, it's not checked yet.

Stackblitz Demo

2 Comments

Since mat-checkbox supports ngModel we don't need a DOM reference variable #c
Yes! you are right. he has modified the question. But even though he don't want ngModel, DOM reference variable is still not needed. You can get it $event object through (change) event
4

Use [checked] & [value] to bind the value, and pass the param in (change) event. I have created an example, check here https://stackblitz.com/edit/angular-anyw41?file=app/checkbox-configurable-example.html

1 Comment

I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
4

You can use the approach mentioned by Kuncevič. To get the exact value you want to use something like this

<section *ngFor="let item of list">
  <mat-checkbox [checked]="item.value" (click)="toggle($event)">{{item.key}}</mat-checkbox>
</section>

And in the typescript use "event.source" to retrieve the value

toggle(event){
  console.log(event.source.value);
}

1 Comment

ERROR TypeError: "event.source is undefined"
4

I solved it this way:

HTML:

<mat-checkbox class="chklist" labelPosition="after" (change)="seleccionRubros($event)">

TS:

public seleccionRubros(event: MatCheckboxChange) {

   if (!event.source.checked) {
      this.subRubrosSelec = [];
   }

};

Notice how I passed $event inside seleccionRubros() in the (change) event, and later on determined that on click, the checkbox was either checked or unchecked by reading event.source.checked.

As a clarification, I decided to specify the type of event but it's not actually needed.

Comments

0

Actually it is possible wihtout ngModel also :)

https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html

1 Comment

hahaha, sorry, did not see that all the other guys that already had posted answers...
-1

Pass $event to the function

<section *ngFor="let item of list">
  <mat-checkbox [checked]="item.value" (click)="toggle($event)">{{item.key}}</mat-checkbox>
</section>

You can get value in function from event object..

toggle(event){
  console.log(event)
}

I think it will be event.value (not sure on this. You can see in console)

Comments

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.