Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

angular - check array values and change input isCheck (function)

I store true checkbox values in an array,

then after getting the array I want to chanage the checkbox isChecked to true if the checkbox value was in the array,

here is the function :

put(array, checkbox){
    for(let ent of checkbox) {    
      for(let entery of array)  {
        ent.val === entery 
        ? ent.isChecked= true
        : ent.isChecked= false
      }
    }
  }

here how did I store checkbox :


  public home_category = [
    { val: '????  ???', isChecked: false },
    { val: '????', isChecked: false },
    { val: '???', isChecked: false },
    { val: '????', isChecked: false },
    { val: '???? ????', isChecked: false },
    { val: '????', isChecked: false },
    { val: '???? ?????', isChecked: false },
    { val: '???? ????', isChecked: false },
    { val: '??????', isChecked: false }
  ];

this function did not work :(

html :

        <ion-list>
          <ion-item *ngFor="let entry of home_category">
            <ion-label>{{entry.val}}</ion-label>
            <ion-checkbox slot="end" [(ngModel)]="entry.isChecked"></ion-checkbox>
          </ion-item>
        </ion-list>
question from:https://stackoverflow.com/questions/65713710/check-array-values-and-change-input-ischeck-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:

put(array, checkbox) {
    checkbox.forEach(chk => chk.isChecked = array.includes(chk.val));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...