How to mark check box as a checked in angular 4












2














I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like



 <tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>


i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript



 document.getElementById("role").checked


but in angular 4 there is no property like that.



i searched and found there is one property binding for




[checked] ="somevariable"




but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.



other solution I have in jquery like



$(document.getElementById(role)).prop('checked', true);


but might be it can create problem, i am not sure please correct me.



please help me. any clue or logic will same my days.










share|improve this question


















  • 1




    Make sameVariable a property of roleObj, and only update the relevant ones
    – bugs
    Apr 18 at 16:23
















2














I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like



 <tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>


i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript



 document.getElementById("role").checked


but in angular 4 there is no property like that.



i searched and found there is one property binding for




[checked] ="somevariable"




but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.



other solution I have in jquery like



$(document.getElementById(role)).prop('checked', true);


but might be it can create problem, i am not sure please correct me.



please help me. any clue or logic will same my days.










share|improve this question


















  • 1




    Make sameVariable a property of roleObj, and only update the relevant ones
    – bugs
    Apr 18 at 16:23














2












2








2







I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like



 <tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>


i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript



 document.getElementById("role").checked


but in angular 4 there is no property like that.



i searched and found there is one property binding for




[checked] ="somevariable"




but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.



other solution I have in jquery like



$(document.getElementById(role)).prop('checked', true);


but might be it can create problem, i am not sure please correct me.



please help me. any clue or logic will same my days.










share|improve this question













I am very new in angular 2. i need to mark check box on a button click.
i have some checkbox in a loop like



 <tr *ngFor="let roleObj of roleNameList">
<td>
<input type="checkbox" id ={{roleObj.roleID}} />
</td>
<td>{{roleObj.roleName}}</td>
</tr>


i have one array of selected role, only i need to mark those check boxes on a edit button click . so what i did like same in javascript



 document.getElementById("role").checked


but in angular 4 there is no property like that.



i searched and found there is one property binding for




[checked] ="somevariable"




but the problem is same property [checked] ="somevariable" will add on all checkboxes. the result is when i assigned the somevariable as true. it will mark all the checkboxes.



other solution I have in jquery like



$(document.getElementById(role)).prop('checked', true);


but might be it can create problem, i am not sure please correct me.



please help me. any clue or logic will same my days.







angular angular4-forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 18 at 16:20









user3364549

12217




12217








  • 1




    Make sameVariable a property of roleObj, and only update the relevant ones
    – bugs
    Apr 18 at 16:23














  • 1




    Make sameVariable a property of roleObj, and only update the relevant ones
    – bugs
    Apr 18 at 16:23








1




1




Make sameVariable a property of roleObj, and only update the relevant ones
– bugs
Apr 18 at 16:23




Make sameVariable a property of roleObj, and only update the relevant ones
– bugs
Apr 18 at 16:23












3 Answers
3






active

oldest

votes


















6














You can modify your object to also include a boolean checked property (i.e. roleObj.checked = false) and dynamically update the relevant ones when you need to.



Then you markup simply becomes




<input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />






share|improve this answer























  • thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
    – user3364549
    Apr 19 at 5:37










  • Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
    – bugs
    Apr 19 at 7:13



















1














You can follow this simple implementation



import { Component } from '@angular/core';

@Component({
selector: 'my-app',
template: `
<ng-container *ngFor="let item of items">
<input type="checkbox"
[checked]="selected === item.id "
[value]="item.id"
(change)="selected = item.id"
[attr.id]="item.id"
/>
<label [attr.for]="item.id"> {{ item.label }}</label>
</ng-container>

`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
items = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' }
];

selected = 1;
}


Live demo






share|improve this answer





























    1














    You should use variable. In your .ts:



    public checkboxValue: boolean;
    constructor() {
    this.checkboxValue = false;
    }


    And in your .html:



     <input type="checkbox" [(ngModel)]="checkboxValue"/>


    You should remember to add FormModule in your @NgModule:



    import { FormsModule, ReactiveFormsModule } from '@angular/forms';



    @NgModule({
    imports: [
    FormsModule
    ]





    share|improve this answer























      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%2f49904531%2fhow-to-mark-check-box-as-a-checked-in-angular-4%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









      6














      You can modify your object to also include a boolean checked property (i.e. roleObj.checked = false) and dynamically update the relevant ones when you need to.



      Then you markup simply becomes




      <input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />






      share|improve this answer























      • thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
        – user3364549
        Apr 19 at 5:37










      • Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
        – bugs
        Apr 19 at 7:13
















      6














      You can modify your object to also include a boolean checked property (i.e. roleObj.checked = false) and dynamically update the relevant ones when you need to.



      Then you markup simply becomes




      <input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />






      share|improve this answer























      • thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
        – user3364549
        Apr 19 at 5:37










      • Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
        – bugs
        Apr 19 at 7:13














      6












      6








      6






      You can modify your object to also include a boolean checked property (i.e. roleObj.checked = false) and dynamically update the relevant ones when you need to.



      Then you markup simply becomes




      <input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />






      share|improve this answer














      You can modify your object to also include a boolean checked property (i.e. roleObj.checked = false) and dynamically update the relevant ones when you need to.



      Then you markup simply becomes




      <input type="checkbox" [checked]="roleObj.checked" id ={{roleObj.roleID}} />







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 10 at 23:51

























      answered Apr 18 at 16:26









      bugs

      4,9665723




      4,9665723












      • thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
        – user3364549
        Apr 19 at 5:37










      • Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
        – bugs
        Apr 19 at 7:13


















      • thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
        – user3364549
        Apr 19 at 5:37










      • Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
        – bugs
        Apr 19 at 7:13
















      thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
      – user3364549
      Apr 19 at 5:37




      thanks for your reply. i got you logic but could you please correct me why we should not use jquery logic
      – user3364549
      Apr 19 at 5:37












      Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
      – bugs
      Apr 19 at 7:13




      Well, given that you are building an Angular application, I would try to keep it as clean and angular-y as possible. Everything you can do with jQuery, you can do with standard Angular logic (pretty much) so I would try not to mix the two. The Angular version is also usually much cleaner, readable and reusable than a jQuery alternative
      – bugs
      Apr 19 at 7:13













      1














      You can follow this simple implementation



      import { Component } from '@angular/core';

      @Component({
      selector: 'my-app',
      template: `
      <ng-container *ngFor="let item of items">
      <input type="checkbox"
      [checked]="selected === item.id "
      [value]="item.id"
      (change)="selected = item.id"
      [attr.id]="item.id"
      />
      <label [attr.for]="item.id"> {{ item.label }}</label>
      </ng-container>

      `,
      styleUrls: ['./app.component.css']
      })
      export class AppComponent {
      items = [
      { id: 1, label: 'one' },
      { id: 2, label: 'two' },
      { id: 3, label: 'three' }
      ];

      selected = 1;
      }


      Live demo






      share|improve this answer


























        1














        You can follow this simple implementation



        import { Component } from '@angular/core';

        @Component({
        selector: 'my-app',
        template: `
        <ng-container *ngFor="let item of items">
        <input type="checkbox"
        [checked]="selected === item.id "
        [value]="item.id"
        (change)="selected = item.id"
        [attr.id]="item.id"
        />
        <label [attr.for]="item.id"> {{ item.label }}</label>
        </ng-container>

        `,
        styleUrls: ['./app.component.css']
        })
        export class AppComponent {
        items = [
        { id: 1, label: 'one' },
        { id: 2, label: 'two' },
        { id: 3, label: 'three' }
        ];

        selected = 1;
        }


        Live demo






        share|improve this answer
























          1












          1








          1






          You can follow this simple implementation



          import { Component } from '@angular/core';

          @Component({
          selector: 'my-app',
          template: `
          <ng-container *ngFor="let item of items">
          <input type="checkbox"
          [checked]="selected === item.id "
          [value]="item.id"
          (change)="selected = item.id"
          [attr.id]="item.id"
          />
          <label [attr.for]="item.id"> {{ item.label }}</label>
          </ng-container>

          `,
          styleUrls: ['./app.component.css']
          })
          export class AppComponent {
          items = [
          { id: 1, label: 'one' },
          { id: 2, label: 'two' },
          { id: 3, label: 'three' }
          ];

          selected = 1;
          }


          Live demo






          share|improve this answer












          You can follow this simple implementation



          import { Component } from '@angular/core';

          @Component({
          selector: 'my-app',
          template: `
          <ng-container *ngFor="let item of items">
          <input type="checkbox"
          [checked]="selected === item.id "
          [value]="item.id"
          (change)="selected = item.id"
          [attr.id]="item.id"
          />
          <label [attr.for]="item.id"> {{ item.label }}</label>
          </ng-container>

          `,
          styleUrls: ['./app.component.css']
          })
          export class AppComponent {
          items = [
          { id: 1, label: 'one' },
          { id: 2, label: 'two' },
          { id: 3, label: 'three' }
          ];

          selected = 1;
          }


          Live demo







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 18 at 16:28









          Tomasz Kula

          6,5591331




          6,5591331























              1














              You should use variable. In your .ts:



              public checkboxValue: boolean;
              constructor() {
              this.checkboxValue = false;
              }


              And in your .html:



               <input type="checkbox" [(ngModel)]="checkboxValue"/>


              You should remember to add FormModule in your @NgModule:



              import { FormsModule, ReactiveFormsModule } from '@angular/forms';



              @NgModule({
              imports: [
              FormsModule
              ]





              share|improve this answer




























                1














                You should use variable. In your .ts:



                public checkboxValue: boolean;
                constructor() {
                this.checkboxValue = false;
                }


                And in your .html:



                 <input type="checkbox" [(ngModel)]="checkboxValue"/>


                You should remember to add FormModule in your @NgModule:



                import { FormsModule, ReactiveFormsModule } from '@angular/forms';



                @NgModule({
                imports: [
                FormsModule
                ]





                share|improve this answer


























                  1












                  1








                  1






                  You should use variable. In your .ts:



                  public checkboxValue: boolean;
                  constructor() {
                  this.checkboxValue = false;
                  }


                  And in your .html:



                   <input type="checkbox" [(ngModel)]="checkboxValue"/>


                  You should remember to add FormModule in your @NgModule:



                  import { FormsModule, ReactiveFormsModule } from '@angular/forms';



                  @NgModule({
                  imports: [
                  FormsModule
                  ]





                  share|improve this answer














                  You should use variable. In your .ts:



                  public checkboxValue: boolean;
                  constructor() {
                  this.checkboxValue = false;
                  }


                  And in your .html:



                   <input type="checkbox" [(ngModel)]="checkboxValue"/>


                  You should remember to add FormModule in your @NgModule:



                  import { FormsModule, ReactiveFormsModule } from '@angular/forms';



                  @NgModule({
                  imports: [
                  FormsModule
                  ]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 18 at 16:54









                  Zach Newburgh

                  35729




                  35729










                  answered Apr 18 at 16:23









                  kris_IV

                  484521




                  484521






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f49904531%2fhow-to-mark-check-box-as-a-checked-in-angular-4%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







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud