How to get the value from angular material checkbox










0















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?










share|improve this question
























  • this value _value_ ?

    – SergioEscudero
    Nov 13 '18 at 13:11











  • @SergioEscudero just modified the question.

    – sreginogemoh
    Nov 13 '18 at 13:14















0















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?










share|improve this question
























  • this value _value_ ?

    – SergioEscudero
    Nov 13 '18 at 13:11











  • @SergioEscudero just modified the question.

    – sreginogemoh
    Nov 13 '18 at 13:14













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












5 Answers
5






active

oldest

votes


















1















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>





share|improve this answer
































    1














    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






    share|improve this answer

























    • 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











    • 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



















    0














    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)






    share|improve this answer
































      0














      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






      share|improve this answer

























      • 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


















      0














      Actually it is possible wihtout ngModel also :)



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






      share|improve this answer























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

        – Magnus Gudmundsson
        Nov 13 '18 at 13:34










      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
      );



      );













      draft saved

      draft discarded


















      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









      1















      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>





      share|improve this answer





























        1















        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>





        share|improve this answer



























          1












          1








          1








          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>





          share|improve this answer
















          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>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 13:32

























          answered Nov 13 '18 at 13:27









          KuncevičKuncevič

          9,65484876




          9,65484876























              1














              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






              share|improve this answer

























              • 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











              • 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
















              1














              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






              share|improve this answer

























              • 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











              • 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














              1












              1








              1







              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






              share|improve this answer















              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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '18 at 13:23

























              answered Nov 13 '18 at 13:21









              StavmStavm

              1,90611731




              1,90611731












              • 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











              • 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


















              • 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











              • 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

















              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












              0














              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)






              share|improve this answer





























                0














                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)






                share|improve this answer



























                  0












                  0








                  0







                  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)






                  share|improve this answer















                  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)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 13:19

























                  answered Nov 13 '18 at 13:16









                  Vijay AtminVijay Atmin

                  16410




                  16410





















                      0














                      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






                      share|improve this answer

























                      • 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















                      0














                      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






                      share|improve this answer

























                      • 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













                      0












                      0








                      0







                      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






                      share|improve this answer















                      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







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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

















                      • 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











                      0














                      Actually it is possible wihtout ngModel also :)



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






                      share|improve this answer























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

                        – Magnus Gudmundsson
                        Nov 13 '18 at 13:34















                      0














                      Actually it is possible wihtout ngModel also :)



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






                      share|improve this answer























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

                        – Magnus Gudmundsson
                        Nov 13 '18 at 13:34













                      0












                      0








                      0







                      Actually it is possible wihtout ngModel also :)



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






                      share|improve this answer













                      Actually it is possible wihtout ngModel also :)



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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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

















                      • 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

















                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      27

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      Category:Rhetoric