How to get the value from angular material checkbox
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?
angular checkbox angular-material2
add a comment |
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?
angular checkbox angular-material2
this value_value_?
– SergioEscudero
Nov 13 '18 at 13:11
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14
add a comment |
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?
angular checkbox angular-material2
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?
angular checkbox angular-material2
angular checkbox angular-material2
edited Nov 13 '18 at 13:15
sreginogemoh
asked Nov 13 '18 at 13:08
sreginogemohsreginogemoh
2,448124282
2,448124282
this value_value_?
– SergioEscudero
Nov 13 '18 at 13:11
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14
add a comment |
this value_value_?
– SergioEscudero
Nov 13 '18 at 13:11
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14
this value
_value_ ?– SergioEscudero
Nov 13 '18 at 13:11
this value
_value_ ?– SergioEscudero
Nov 13 '18 at 13:11
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14
add a comment |
5 Answers
5
active
oldest
votes
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>
add a comment |
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
Since mat-checkbox supportsngModelwe don't need a DOM reference variable#c
– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
Yes! you are right. he has modified the question. But even though he don't wantngModel, DOM reference variable is still not needed. You can get it$eventobject through(change)event
– Santhosh V
Nov 13 '18 at 13:27
add a comment |
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)
add a comment |
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
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
add a comment |
Actually it is possible wihtout ngModel also :)
https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281709%2fhow-to-get-the-value-from-angular-material-checkbox%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 13 '18 at 13:32
answered Nov 13 '18 at 13:27
KuncevičKuncevič
9,65484876
9,65484876
add a comment |
add a comment |
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
Since mat-checkbox supportsngModelwe don't need a DOM reference variable#c
– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
Yes! you are right. he has modified the question. But even though he don't wantngModel, DOM reference variable is still not needed. You can get it$eventobject through(change)event
– Santhosh V
Nov 13 '18 at 13:27
add a comment |
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
Since mat-checkbox supportsngModelwe don't need a DOM reference variable#c
– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
Yes! you are right. he has modified the question. But even though he don't wantngModel, DOM reference variable is still not needed. You can get it$eventobject through(change)event
– Santhosh V
Nov 13 '18 at 13:27
add a comment |
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
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
edited Nov 13 '18 at 13:23
answered Nov 13 '18 at 13:21
StavmStavm
1,90611731
1,90611731
Since mat-checkbox supportsngModelwe don't need a DOM reference variable#c
– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
Yes! you are right. he has modified the question. But even though he don't wantngModel, DOM reference variable is still not needed. You can get it$eventobject through(change)event
– Santhosh V
Nov 13 '18 at 13:27
add a comment |
Since mat-checkbox supportsngModelwe don't need a DOM reference variable#c
– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
Yes! you are right. he has modified the question. But even though he don't wantngModel, DOM reference variable is still not needed. You can get it$eventobject through(change)event
– Santhosh V
Nov 13 '18 at 13:27
Since mat-checkbox supports
ngModel we don't need a DOM reference variable #c– Santhosh V
Nov 13 '18 at 13:23
Since mat-checkbox supports
ngModel we don't need a DOM reference variable #c– Santhosh V
Nov 13 '18 at 13:23
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
@SanthoshV he specifically asked without ngModel ...
– Stavm
Nov 13 '18 at 13:24
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– Santhosh V
Nov 13 '18 at 13:27
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– Santhosh V
Nov 13 '18 at 13:27
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 13 '18 at 13:19
answered Nov 13 '18 at 13:16
Vijay AtminVijay Atmin
16410
16410
add a comment |
add a comment |
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
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
add a comment |
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
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
add a comment |
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
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
edited Nov 13 '18 at 13:31
answered Nov 13 '18 at 13:15
Santhosh VSanthosh V
1216
1216
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
add a comment |
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I just updated the question
– sreginogemoh
Nov 13 '18 at 13:15
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
I updated the answer too. It's working as you want. Please check the link stackblitz.com/edit/angular-anyw41?file=app/…
– Santhosh V
Nov 13 '18 at 13:19
add a comment |
Actually it is possible wihtout ngModel also :)
https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
add a comment |
Actually it is possible wihtout ngModel also :)
https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
add a comment |
Actually it is possible wihtout ngModel also :)
https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html
Actually it is possible wihtout ngModel also :)
https://stackblitz.com/edit/angular-anyw41-zxrncz?file=app/checkbox-configurable-example.html
answered Nov 13 '18 at 13:33
Magnus GudmundssonMagnus Gudmundsson
369211
369211
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
add a comment |
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
hahaha, sorry, did not see that all the other guys that already had posted answers...
– Magnus Gudmundsson
Nov 13 '18 at 13:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281709%2fhow-to-get-the-value-from-angular-material-checkbox%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
this value
_value_?– SergioEscudero
Nov 13 '18 at 13:11
@SergioEscudero just modified the question.
– sreginogemoh
Nov 13 '18 at 13:14