Javascript “角度4组件被选中”复选框

Javascript “角度4组件被选中”复选框,javascript,angular,Javascript,Angular,我是Angular的新手,我想获取当前选中的复选框id。我已在中选中了其他线程,但大多数线程都与控制器一起使用,我在组件中使用。 以下是详细信息。 我有这样一个组件 <table class='table table-striped table-hover admin-form' *ngIf="deviceCategories"> <a (click)="test()">test</a> <thead> <tr>

我是Angular的新手,我想获取当前选中的复选框id。我已在中选中了其他线程,但大多数线程都与控制器一起使用,我在组件中使用。
以下是详细信息。 我有这样一个组件

<table class='table table-striped table-hover admin-form' *ngIf="deviceCategories">
<a (click)="test()">test</a>
<thead>
    <tr>
        <td class="text-center">序列</td>
        <th class="text-center">组名</th>
        <th class="text-center">LOGO</th>
        <th class="text-center">操作</th>
    </tr>
</thead>
<tbody>
    <tr *ngFor="let deviceCategory of deviceCategories | paginate: config">
        <td class="center">
            <input type="checkbox" ng-model="selected[deviceCategory.id]" id="checkbox_{{deviceCategory.id}}">
        </td>
        <td class="text-center">{{deviceCategory.id}}</td>
        <td class="text-center">{{deviceCategory.name}}</td>
        <td class="text-center"><img src="{{deviceCategory.logoUrl}}" width="30" alt=""></td>
        <td class="text-center project-actions">
            <a [routerLink]="['/management/deviceCategory/edit/',deviceCategory.id]" class="btn btn-white btn-xs text-muted"> <i class="fa fa-eye text-system"></i> 修改 </a>
            <a (click)="OnDelete(deviceCategory.id)" class="btn btn-white btn-xs text-muted"> <i class="fa fa-eye text-system"></i> 删除 </a>
        </td>
    </tr>
</tbody>

任何帮助都将不胜感激。

您可以使用
[选中]
属性并将变量绑定到它,它应该是一个布尔值

 <input type="checkbox"  [checked]="deviceCategory.checked"  id="checkbox_{{deviceCategory.id}}"
 />
和内部
TS

 getDeviceID(device:any) {
      console.log(device.id);
 }

根据@Sajeetharan的建议,我可以通过以下代码实现我的要求

html

ts代码以获取当前复选框

  console.log(this.deviceCategories.filter(d=>d.checked===true));

复选框在哪里谢谢,我现在已经添加了它,我想获得所选的deviceCategory.id或DeviceCategoria作为旁注:您也在使用
ng模型
哪个AngularJS语法,也许可以检查您的代码,您实际上没有在其他地方使用AngularJS语法…谢谢,我怎样才能在我的ts文件中得到这个选中的id?添加了一个解释谢谢,我可以理解我需要定义一个数组来在getDeviceID方法中存储选中的id,在前面,我可以看到一个演示,它可以很容易地过滤选中的复选框,而不是用这种方式。有可能达到这样的效果吗?如果有,我如何在ts文件中使用下面的代码<代码>$scope.ShowSelected=function(){console.log($scope.selected);警报(JSON.stringify($scope.selected));},我想用这种方式实现我的需求,但我不知道如何在我的组件中使用它。$scope用于angular1,Angular2则使用此选项。selected.idI想显示其中一些选项已被选中。但这并不重要;行不通
 getDeviceID(device:any) {
      console.log(device.id);
 }
   <td class="text-center">
        <input type="checkbox" [(ngModel)]="deviceCategory.checked" />
    </td>
 export interface DeviceCategoryViewModel {
        id?: number;
        name?: string;
        checked?:boolean;
    }
  console.log(this.deviceCategories.filter(d=>d.checked===true));