复选框不起作用。角度2

时间:2018-06-29 03:38:13

标签: angular

在Oracle数据库中,存在这样的参数“活动” ,其数据类型为“ VARCHAR2(1字节)” 。此处使用 Y N 的值。 如果要启用 Y ,我要使复选框处于活动状态,而 N 则不处于活动状态。

我想这样做,但是到处都是复选框:

<table class="table table-hover">
  <thead>
    <tr>
      <th [class.active]="key === 'name'" (click)="setOrder('name')"><b>Name <span [hidden]="reverse">▼</span><span [hidden]="!reverse">▲</span></b></th>
      <th style="text-align: center;" [class.active]="key === 'active'" (click)="setOrder('active')"><b>File <span [hidden]="reverse">▼</span><span [hidden]="!reverse">▲</span> </b></th>
     </tr>
  </thead>
  <tbody>
    <tr *ngFor="let project of projects | orderBy: key:reverse | let projectIndex = index" >
      <td >{{project.name}}</td>
      <td style="text-align: center">
        <input type="checkbox" [checked]="project.active == 'Y'" class="material_checkbox" [(ngModel)]="project.active" name="active_{{projectIndex}}"  disabled />
      </td>
    </tr>
  </tbody>
</table>

如何设置条件,以便如果存在 Y ,则复选框处于活动状态,而如果 N ,则处于非活动状态?

2 个答案:

答案 0 :(得分:0)

我检查了一下,因为您的[ngModel]不是布尔值,所以应该将其删除。只需使用以下解决方案即可:-

[[ClassName.methodName]]

答案 1 :(得分:0)

我认为这是您的基本项目类型:

interface Project {
  name: string,
  active: string;

  // additional properties.
}

如果是,则:

 projectList: Project[];

  constructor() {
    this.populateProjectList();
  }

  populateProjectList(): void {
    this.projectList = [];
    this.projectList.push({name: 'project1', active: 'Y'});
    this.projectList.push({name: 'project2', active: 'N'});
    this.projectList.push({name: 'project3', active: 'Y'});
    this.projectList.push({name: 'project4', active: 'N'});
  }

// In your HTML

<p *ngFor="let project of projectList">
  <label for="">{{project.name}}</label>
  <input type="checkbox" [checked]="project.active == 'Y'"> 
</p>

You can play here for more options.