On change of checkbox state remain same in UI











up vote
1
down vote

favorite












<input type="checkbox" [(ngModel)]="rowData.is_permitted" (change)="changeStatus()">

changeStatus() {
rowData.is_permitted = true;
}


If I uncheck checkbox but conditionally I want to select the checkbox, the flag updated but not affect in UI.










share|improve this question






















  • what is not working ?
    – Sunil Singh
    Nov 17 at 9:05










  • Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
    – Sunil Singh
    Nov 17 at 9:06










  • It doesn't (when you click on Change label.
    – Alon Shmiel
    Nov 17 at 9:08















up vote
1
down vote

favorite












<input type="checkbox" [(ngModel)]="rowData.is_permitted" (change)="changeStatus()">

changeStatus() {
rowData.is_permitted = true;
}


If I uncheck checkbox but conditionally I want to select the checkbox, the flag updated but not affect in UI.










share|improve this question






















  • what is not working ?
    – Sunil Singh
    Nov 17 at 9:05










  • Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
    – Sunil Singh
    Nov 17 at 9:06










  • It doesn't (when you click on Change label.
    – Alon Shmiel
    Nov 17 at 9:08













up vote
1
down vote

favorite









up vote
1
down vote

favorite











<input type="checkbox" [(ngModel)]="rowData.is_permitted" (change)="changeStatus()">

changeStatus() {
rowData.is_permitted = true;
}


If I uncheck checkbox but conditionally I want to select the checkbox, the flag updated but not affect in UI.










share|improve this question













<input type="checkbox" [(ngModel)]="rowData.is_permitted" (change)="changeStatus()">

changeStatus() {
rowData.is_permitted = true;
}


If I uncheck checkbox but conditionally I want to select the checkbox, the flag updated but not affect in UI.







javascript angular html5 typescript checkbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 at 8:57









Shubham Ghormade

276




276












  • what is not working ?
    – Sunil Singh
    Nov 17 at 9:05










  • Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
    – Sunil Singh
    Nov 17 at 9:06










  • It doesn't (when you click on Change label.
    – Alon Shmiel
    Nov 17 at 9:08


















  • what is not working ?
    – Sunil Singh
    Nov 17 at 9:05










  • Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
    – Sunil Singh
    Nov 17 at 9:06










  • It doesn't (when you click on Change label.
    – Alon Shmiel
    Nov 17 at 9:08
















what is not working ?
– Sunil Singh
Nov 17 at 9:05




what is not working ?
– Sunil Singh
Nov 17 at 9:05












Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
– Sunil Singh
Nov 17 at 9:06




Its working as expected. Check here stackblitz.com/edit/angular-1vmnl2
– Sunil Singh
Nov 17 at 9:06












It doesn't (when you click on Change label.
– Alon Shmiel
Nov 17 at 9:08




It doesn't (when you click on Change label.
– Alon Shmiel
Nov 17 at 9:08












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










The problem is shown in this stackblitz. After unchecking the checkbox, the value is set to true in code but the checkbox remains unchecked.





You can do the following to override the action of the user with a change in code:




  • Force an immediate change detection by calling ChangeDetectorRef.detectChanges

  • Set the value to true

  • Let the change detection mecanism update the checkbox to reflect the updated value


In the code below, I handle the ngModelChange event. The conditional behavior mentioned in the question is simulated with the keepChecked property:



<input type="checkbox" 
[(ngModel)]="rowData.is_permitted"
(ngModelChange)="changeStatus()">




changeStatus() {
if (this.keepChecked) {
this.changeDetectorRef.detectChanges();
this.rowData.is_permitted = true;
}
}


See this stackblitz for a demo.





A similar result could be obtained by setting the value asynchronously in a setTimeout callback:



changeStatus() {
if (this.keepChecked) {
setTimeout(() => {
this.rowData.is_permitted = true;
});
}
}


but I prefer to keep the code synchronous by calling ChangeDetectorRef.detectChanges.






share|improve this answer






























    up vote
    0
    down vote













    If I understand your question,
    After changing the input, it remains true.



    So, when rowData.is_permitted is true, you need to make it false.



    rowData.is_permitted is false, you need to make it true.



    If this is your problem, try do it by changing changeStatus function:



    rowData.is_permitted = !rowData.is_permitted;


    rowData.is_permitted will get the opposite value.



    Working example that I forked from @SunilSingh is here






    share|improve this answer




























      up vote
      0
      down vote













      You have to change ngModel value on click. May be rowData.is_permitted default value is true so, on click it is not changed by your function.
      Your changeStatus function will be



      changeStatus() {
      rowData.is_permitted = !rowData.is_permitted;
      }





      share|improve this answer










      New contributor




      Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















        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',
        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%2f53349706%2fon-change-of-checkbox-state-remain-same-in-ui%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        The problem is shown in this stackblitz. After unchecking the checkbox, the value is set to true in code but the checkbox remains unchecked.





        You can do the following to override the action of the user with a change in code:




        • Force an immediate change detection by calling ChangeDetectorRef.detectChanges

        • Set the value to true

        • Let the change detection mecanism update the checkbox to reflect the updated value


        In the code below, I handle the ngModelChange event. The conditional behavior mentioned in the question is simulated with the keepChecked property:



        <input type="checkbox" 
        [(ngModel)]="rowData.is_permitted"
        (ngModelChange)="changeStatus()">




        changeStatus() {
        if (this.keepChecked) {
        this.changeDetectorRef.detectChanges();
        this.rowData.is_permitted = true;
        }
        }


        See this stackblitz for a demo.





        A similar result could be obtained by setting the value asynchronously in a setTimeout callback:



        changeStatus() {
        if (this.keepChecked) {
        setTimeout(() => {
        this.rowData.is_permitted = true;
        });
        }
        }


        but I prefer to keep the code synchronous by calling ChangeDetectorRef.detectChanges.






        share|improve this answer



























          up vote
          1
          down vote



          accepted










          The problem is shown in this stackblitz. After unchecking the checkbox, the value is set to true in code but the checkbox remains unchecked.





          You can do the following to override the action of the user with a change in code:




          • Force an immediate change detection by calling ChangeDetectorRef.detectChanges

          • Set the value to true

          • Let the change detection mecanism update the checkbox to reflect the updated value


          In the code below, I handle the ngModelChange event. The conditional behavior mentioned in the question is simulated with the keepChecked property:



          <input type="checkbox" 
          [(ngModel)]="rowData.is_permitted"
          (ngModelChange)="changeStatus()">




          changeStatus() {
          if (this.keepChecked) {
          this.changeDetectorRef.detectChanges();
          this.rowData.is_permitted = true;
          }
          }


          See this stackblitz for a demo.





          A similar result could be obtained by setting the value asynchronously in a setTimeout callback:



          changeStatus() {
          if (this.keepChecked) {
          setTimeout(() => {
          this.rowData.is_permitted = true;
          });
          }
          }


          but I prefer to keep the code synchronous by calling ChangeDetectorRef.detectChanges.






          share|improve this answer

























            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            The problem is shown in this stackblitz. After unchecking the checkbox, the value is set to true in code but the checkbox remains unchecked.





            You can do the following to override the action of the user with a change in code:




            • Force an immediate change detection by calling ChangeDetectorRef.detectChanges

            • Set the value to true

            • Let the change detection mecanism update the checkbox to reflect the updated value


            In the code below, I handle the ngModelChange event. The conditional behavior mentioned in the question is simulated with the keepChecked property:



            <input type="checkbox" 
            [(ngModel)]="rowData.is_permitted"
            (ngModelChange)="changeStatus()">




            changeStatus() {
            if (this.keepChecked) {
            this.changeDetectorRef.detectChanges();
            this.rowData.is_permitted = true;
            }
            }


            See this stackblitz for a demo.





            A similar result could be obtained by setting the value asynchronously in a setTimeout callback:



            changeStatus() {
            if (this.keepChecked) {
            setTimeout(() => {
            this.rowData.is_permitted = true;
            });
            }
            }


            but I prefer to keep the code synchronous by calling ChangeDetectorRef.detectChanges.






            share|improve this answer














            The problem is shown in this stackblitz. After unchecking the checkbox, the value is set to true in code but the checkbox remains unchecked.





            You can do the following to override the action of the user with a change in code:




            • Force an immediate change detection by calling ChangeDetectorRef.detectChanges

            • Set the value to true

            • Let the change detection mecanism update the checkbox to reflect the updated value


            In the code below, I handle the ngModelChange event. The conditional behavior mentioned in the question is simulated with the keepChecked property:



            <input type="checkbox" 
            [(ngModel)]="rowData.is_permitted"
            (ngModelChange)="changeStatus()">




            changeStatus() {
            if (this.keepChecked) {
            this.changeDetectorRef.detectChanges();
            this.rowData.is_permitted = true;
            }
            }


            See this stackblitz for a demo.





            A similar result could be obtained by setting the value asynchronously in a setTimeout callback:



            changeStatus() {
            if (this.keepChecked) {
            setTimeout(() => {
            this.rowData.is_permitted = true;
            });
            }
            }


            but I prefer to keep the code synchronous by calling ChangeDetectorRef.detectChanges.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 17 at 15:11

























            answered Nov 17 at 14:00









            ConnorsFan

            28k42457




            28k42457
























                up vote
                0
                down vote













                If I understand your question,
                After changing the input, it remains true.



                So, when rowData.is_permitted is true, you need to make it false.



                rowData.is_permitted is false, you need to make it true.



                If this is your problem, try do it by changing changeStatus function:



                rowData.is_permitted = !rowData.is_permitted;


                rowData.is_permitted will get the opposite value.



                Working example that I forked from @SunilSingh is here






                share|improve this answer

























                  up vote
                  0
                  down vote













                  If I understand your question,
                  After changing the input, it remains true.



                  So, when rowData.is_permitted is true, you need to make it false.



                  rowData.is_permitted is false, you need to make it true.



                  If this is your problem, try do it by changing changeStatus function:



                  rowData.is_permitted = !rowData.is_permitted;


                  rowData.is_permitted will get the opposite value.



                  Working example that I forked from @SunilSingh is here






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    If I understand your question,
                    After changing the input, it remains true.



                    So, when rowData.is_permitted is true, you need to make it false.



                    rowData.is_permitted is false, you need to make it true.



                    If this is your problem, try do it by changing changeStatus function:



                    rowData.is_permitted = !rowData.is_permitted;


                    rowData.is_permitted will get the opposite value.



                    Working example that I forked from @SunilSingh is here






                    share|improve this answer












                    If I understand your question,
                    After changing the input, it remains true.



                    So, when rowData.is_permitted is true, you need to make it false.



                    rowData.is_permitted is false, you need to make it true.



                    If this is your problem, try do it by changing changeStatus function:



                    rowData.is_permitted = !rowData.is_permitted;


                    rowData.is_permitted will get the opposite value.



                    Working example that I forked from @SunilSingh is here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 17 at 9:07









                    Alon Shmiel

                    1,0781565113




                    1,0781565113






















                        up vote
                        0
                        down vote













                        You have to change ngModel value on click. May be rowData.is_permitted default value is true so, on click it is not changed by your function.
                        Your changeStatus function will be



                        changeStatus() {
                        rowData.is_permitted = !rowData.is_permitted;
                        }





                        share|improve this answer










                        New contributor




                        Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






















                          up vote
                          0
                          down vote













                          You have to change ngModel value on click. May be rowData.is_permitted default value is true so, on click it is not changed by your function.
                          Your changeStatus function will be



                          changeStatus() {
                          rowData.is_permitted = !rowData.is_permitted;
                          }





                          share|improve this answer










                          New contributor




                          Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You have to change ngModel value on click. May be rowData.is_permitted default value is true so, on click it is not changed by your function.
                            Your changeStatus function will be



                            changeStatus() {
                            rowData.is_permitted = !rowData.is_permitted;
                            }





                            share|improve this answer










                            New contributor




                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            You have to change ngModel value on click. May be rowData.is_permitted default value is true so, on click it is not changed by your function.
                            Your changeStatus function will be



                            changeStatus() {
                            rowData.is_permitted = !rowData.is_permitted;
                            }






                            share|improve this answer










                            New contributor




                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer








                            edited Nov 17 at 10:03









                            Saqib Shahzad

                            534119




                            534119






                            New contributor




                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered Nov 17 at 9:46









                            Shubham Keshari

                            444




                            444




                            New contributor




                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Shubham Keshari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349706%2fon-change-of-checkbox-state-remain-same-in-ui%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

                                "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                Alcedinidae

                                Origin of the phrase “under your belt”?