【问题标题】:How to dynamically bind the value of a checkbox Angular 2如何动态绑定复选框Angular 2的值
【发布时间】:2016-05-05 13:16:04
【问题描述】:

大家好:我有一个组件。组件视图有一个表格:

<div class="container">
<h2>Recipients List</h2>
<br>
<table class="table table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Phone</th>
            <th>Status</th>
            <th>Select</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="#rp of arrAll">   
            <td>{{rp.id}}</td>             
            <td>{{rp.name}}</td>
            <td>{{rp.phone}}</td>                
            <td>{{rp.isActivated?'Active':'Inactive'}}</td>             
            <td>
                <input #{{rp.id}}  type="checkbox" (change)="checkbox(value)">
            </td>
        </tr>          
    </tbody>
</table>

<button class="btn btn-success" (click)="newRecipient()">New Recipient</button>
<button class="btn btn-danger pull-right" (click) ="deleteRecipients()">Delete Recipients</button>

这是使用 *ngFor 生成的table view 图像的链接。

业务逻辑是“当单击删除按钮时,必须删除所有选中的收件人”。我想将一个参数传递给我的删除函数(我认为它应该是一个包含已检查收件人 ID 的数组)

这是我的组件代码:

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import { Router, RouteParams } from 'angular2/router';
import {RecipientsService}    from'../../services/recipients/recipients.service';
import {Recipient} from '../../models/recipient/recipient';

@Component({
selector: 'recipient-list-view',
templateUrl: './app/components/recipient-list-view/recipient-list-view.html',
styleUrls: ["./app/components/recipient-list-view/style.css"]
})

export class RecipientListViewComponent {

arrAll: Recipient[];

constructor(private recipientsService: RecipientsService, params:    RouteParams, private router: Router) {
    this.arrAll = recipientsService.getAll();        
}

newRecipient() {
    this.router.navigate(['../NewRecipient']);
}

deleteRecipients() {  //need to know which recipients are checked

    console.log("delete");
}


}

我想知道如何实现。

干杯

【问题讨论】:

  • #{{rp.id}} 是这个语法

标签: angular angular2-directives


【解决方案1】:

在您的控制器中添加一个功能,该功能将在选中复选框时触发(ng-click of checkbox)。根据复选框的值,您会知道它是否被选中/取消选中。为检查的收件人列表维护另一个模型。如果选中则将收件人附加到列表中,如果未选中则删除。调用 delete 时只删除该模型中的那些?

【讨论】:

    【解决方案2】:

    将属性selected 添加到您的收件人。在复选框更改时,将其设置为 true。

    一旦用户点击删除所有收件人,只需过滤列表中的选定收件人。

    类似这样的:

    <div class="container">
        <h2>Recipients List</h2>
        <br>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Status</th>
                    <th>Select</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="#rp of arrAll">   
                    <td>{{rp.id}}</td>             
                    <td>{{rp.name}}</td>
                    <td>{{rp.phone}}</td>                
                    <td>{{rp.isActivated?'Active':'Inactive'}}</td>             
                    <td>
                        <input #{{rp.id}}  [(ngModel)]="rp.selected" type="checkbox" (change)="checkbox(rp)">
                    </td>
                </tr>          
            </tbody>
        </table>
    
        <button class="btn btn-success" (click)="newRecipient()">New Recipient</button>
        <button class="btn btn-danger pull-right" (click) ="deleteRecipients()">Delete Recipients</button>
    </div>
    

    以及组件:

    export class RecipientListViewComponent {
    
         arrAll: Recipient[];
    
         constructor(private recipientsService: RecipientsService, params: RouteParams, private router: Router) {
            this.arrAll = recipientsService.getAll();        
        }
    
        newRecipient() {
            this.router.navigate(['../NewRecipient']);
        }
    
        deleteRecipients() {  //need to know which recipients are checked
            let selected = this.arrAll.filter((x) => x.selected)
            console.log(selected);
        }
    
        checkbox(recipient) {
            recipient.selected = (recipient.selected) ? false : true;
        }
    }
    

    【讨论】:

    • 为简单起见:recipient.selected = !receipient.selected;
    【解决方案3】:

    如果您可以向“收件人”模型添加更多属性,那么跟踪所选记录非常容易。

    在您的 Recipient 模型中添加了一个“selected”属性,并通过两种方式将复选框绑定到 selected 属性。

    <input #{{rp.id}}  type="checkbox" [(ngModel)]="rp.selected">
    

    现在在组件中添加一个方法来获取所有选中的记录

    private getSelected() {
        return this.arrAll.filter((item) => item.selected);
    }
    

    然后获取选中的记录

    deleteRecipients() {  
        console.log(this.getSelected());
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 2017-06-02
      • 2019-06-13
      • 2019-05-29
      • 2018-09-13
      • 2019-06-12
      • 2017-12-01
      • 1970-01-01
      • 2017-03-06
      相关资源
      最近更新 更多