title: Make a checkbox checked if its id is present in an array
body: <p>I'm creating an app in Angular 8.</p>
  
  <p>My app uses a dialog to show a table which contains data about products, the table shows two columns [id, description], as you can see, the id column generates a checkbox and prints and id for each product.</p>
  
  <p><a href="https://i.stack.imgur.com/gpU9K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gpU9K.png" alt="enter image description here"></a></p>
  
  <p>This is the code that generates each checkbox and prints the ids in the table:</p>
  
  <pre><code>&lt;ng-container matColumnDef="Id"&gt;
      &lt;th mat-header-cell *matHeaderCellDef&gt;Id&lt;/th&gt;
      &lt;td mat-cell *matCellDef="let element"&gt;
      //I assign an id to each checkbox which is the one of the element to show (A, B, C... etc)
      // and bind the method change to 'checkboxChanged', this event lets me to push the id of the checkbox to an array
          &lt;mat-checkbox (change)="checkboxChanged($event)" id={{element.id}}&gt;
              {{element.id}}
          &lt;/mat-checkbox&gt;
      &lt;/td&gt;
  &lt;/ng-container&gt;
  &lt;ng-container matColumnDef="Description"&gt;
      &lt;th mat-header-cell *matHeaderCellDef&gt;Description&lt;/th&gt;
      &lt;td mat-cell *matCellDef="let element"&gt;{{element.description}}&lt;/td&gt;
  &lt;/ng-container&gt;
  </code></pre>
  
  <p>When the user clicks on each checkbox, a function called 'checkboxChanged' is fired to add/remove the id in a array (depending on the state of the checkbox).</p>
  
  <p>This is the code for the logic to add the checkbox to an array:</p>
  
  <pre><code>public tempData = [];
  
  checkboxChanged(event) {
      const id = event.source.id; //Get the id of the checkbox
      console.log(event.checked); //true or false
      console.log(id); //A, B, C... etc
      if (event.checked) this.tempData.push(id); //If checked, add to array
      else { //if unchecked, remove from the array
          const i = this.tempData.indexOf(id);
          this.tempData.splice(i, 1);
      }
  console.log("tempData", this.tempData); // prinst [B, C] from the example of the screenshot 
  }
  </code></pre>
  
  <p>After the user clicks on all the checkboxs of the products they want, they can click on 'add' to send the array to the principal screen and then process it. E. G. if the user clicks on B and C as the screenshot, the array is [B, C]. </p>
  
  <p>The matter is that this dialog uses Angular Material Pagination, so each time the user changes the page all the checkboxes are turn to unchecked as default, so, if I click on A and B, turn to the 2nd page and then return to the first one, the A and B checkboxes will be unchecked but the array will still be [A, B].</p>
  
  <p>What I imagine for this is to make a condition when generating each checkbox so it doesn't matter if I move to another pages. This condition should be 'if the Id already exist in tempData then check the ckeckbox', something like:</p>
  
  <pre><code>&lt;mat-checkbox (change)="checkboxChanged($event)" id={{element.id}} checked = "{{this.tempData}}.includes({{element.id}})"&gt;
      {{element.id}}
  &lt;/mat-checkbox&gt;
  </code></pre>
  
  <p>So the central point here is to discover how to apply this condition, maybe through an *ngIf or through the 'checked' property of the checkbox.</p>
  
  <p>Could anybody help me please?:(</p>