简体   繁体   English

如何保持复选框是否已使用Angular2选中

[英]how to keep the checkbox checked if it has been already checked using Angular2

I have a input checkbox field, when i check the box the functions work fine but after refresh the tick mark disappears in the box. 我有一个输入复选框字段,当我check该复选框时,功能可以正常工作,但刷新后,复选框中的勾号消失了。 So can i get the tick mark be true if it has been already selected and the data saved in other page. 因此,如果已经选择了刻度线并将数据保存在其他页面中,我能否使刻度线为true

HTML: HTML:

<td><input type="checkbox" [(ngModel)]="pin.checked" (change)="fav(pin)" style="margin-top: 10px;"></td>

TS: TS:

loadAllPins() {
  var favouratePins = this.storage.favouratePins; 
  console.log(favouratePins);
  this.ApiService
      .getAllPins(this.guide_id)
      .subscribe(
        pins  => {
          if(favouratePins && favouratePins.length > 0) {
            pins.data.map(function(pin) {
              favouratePins.map(function(item) {
                if(pin._id == item._id) {
                  pin.checked = true;
                }
              })
            })            
          }
          this.pins = pins.data;

        },error => {
          console.log(error);
        });
}

in the first page what all i have checked had been and saved in another page, so the items which have been stored already in another page contents needs to be marked checked in the first page 在第一页中,我已检查的所有内容已保存并保存在另一页中,因此,已经在另一页内容中存储的项目需要在第一页中标记为已选中

You will have to add checked attribute. 您将必须添加checked属性。

<td>
   <input type="checkbox" [checked]="pin.checked" [(ngModel)]="pin.checked" (change)="fav(pin)" style="margin-top: 10px;">
</td>

From your question you are sending pin_id as data to api so, you need to compare pin_id instead of _id 根据您的问题,您正在将pin_id作为data发送到api因此,您需要比较pin_id而不是_id

Here you are sending pin_id var data = { "user_id":user_id, "pin_id":pin._id } 在这里,您要发送pin_id var data = {“ user_id”:user_id,“ pin_id”:pin._id}

So you should use, 所以你应该使用

 if(favouratePins && favouratePins.length > 0) {
    pins.data.map(function(pin) {
      favouratePins.map(function(item) {
       if(pin._id == item._id) {
            pin.checked = true;
        }
       })
      })            
     }
     this.pins = pins.data;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM