【问题标题】:Get values from a dynamic checkbox list从动态复选框列表中获取值
【发布时间】:2017-02-18 13:11:14
【问题描述】:

查看我的html

<ion-item ion-item *ngFor="let item of items">
    <ion-label>{{ item.name }}</ion-label>
    <ion-checkbox color="secondary"></ion-checkbox>
</ion-item>

我的.TS

this.database.executeSql("SELECT * FROM check_item", []).then((data) => {
        this.items = [];
        if (data.rows.length > 0) {
            for (var i = 0; i < data.rows.length; i++) {
                this.items.push({ id: data.rows.item(i).id, name: data.rows.item(i).name });
            }
        }
    }, (error) => {
        console.log("ERROR: " + JSON.stringify(error));
    });

我想从复选框中检索值并将其保存到表格中。如何从动态复选框列表中获取值?

【问题讨论】:

    标签: angular typescript checkbox ionic2


    【解决方案1】:
    <ion-item ion-item *ngFor="let item of items;let i=index">
        <ion-label>{{ item.name }}</ion-label>
        <ion-checkbox [(ngModel)]="checkedItems[i]" color="secondary"></ion-checkbox>
    </ion-item>
    

    其中checkedItems 是一个与items 大小相同的数组,您需要准备和初始化它。

    checkedItems:boolean[];
    
    constructor() { // or whereever `items` is initialized
      this.items = ...
      this.checkedItems = new Array(this.items.length);
    }
    

    【讨论】:

    • 所以,现在我的项目名称没有显示在列表中。会是什么?
    • 抱歉,我将[(ngModel)]... 添加到了错误的行。它应该在&lt;ion-checkbox&gt; 上。您是在导入FormsModule 吗?您收到错误消息吗?
    • 我之前评论中的问题呢?
    • 抱歉,我的标签页仍然打开,但没有时间重新访问。很高兴听到你可以让它工作。
    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2019-06-08
    • 2021-07-01
    • 2019-08-05
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多