mercredi 22 novembre 2017

How to count how many check boxes checked in Angular 2 typescript?

How to count how many boxes checked in Angular 2/4 using typescript. It should show the count of the check boxes checked, and if unchecked it should decrement to 0.

I know how to do it using foreach function in Angularjs, but cant understand how to do this in Typescript, generally I have to replace angular foreach method with Typescript foreach method? Need help

app.ts

  data = [
     {"name":"jason"},
     {"name":"james"},
     {"name":"josh"},
     {"name":"joshua"}
          ];



calculateChecked = function() {            
 let count = 0;

  angular.forEach(this.data, function(value) {    //don't work in typescript
    if(value.checked)
      count++;
  });

  return count;
   };
  };

app.htm

 <li class="list-group-item" *ngFor="let user of data">
    <input type="checkbox" [ngModel]="user.checked"/>
    
    </li>


    <p>total checked: </p>

should show:

enter image description here

And if unchecked should show 0




Aucun commentaire:

Enregistrer un commentaire