2017-06-21 67 views
0
I have the following code on angular 
<div *ngFor="let student of students.controls; let i=index" [formGroupName]="i" [class.active]="student.checked"> 
    <!-- The repeated address template --> 
    <h4>Student #{{i + 1}} 
     <input type="checkbox" formControlName="checked" [(ngModel)]="student.checked"> 
    </h4> 
    <div class="form-group"> 
     <label class="center-block">First Name: 
      <input class="form-control" formControlName="firstName"> 
     </label> 
    </div> 
    <div class="form-group"> 
     <label class="center-block">Last name: 
     <input class="form-control" formControlName="lastName"> 
     </label> 
    /div> 
</div> 

here is my css 

`div.active{ 
    background-color:#CCFFCC !important; 

} 
` 

在该行 formControl和ngModel

当复选框被选中,它包含复选框我的数组元素的背景颜色变成绿色,我想,但formControlName“选中”未考虑到的问题是帐户,当我删除[(ngModel)] =“student.checked”我没有背景颜色改变行为了,但formControlName“checked”工程

实际行为,我建立我的数组与导入的学生属性检查为真,该框不被检查,但当我检查它时,背景变成g reen

通缉行为:我建立我的数组与导入的学生的属性检查真正的,框中检查,当我取消选中它的绿色背景消失(我ngModel [(ngModel)] =“student.checked”是与formControlName “选中”)

回答

0

我已经找到了解决办法

<div *ngFor="let student of students.controls; let i=index" 
[formGroupName]="i" [class.active]="student.controls.checked.value> 
    <!-- The repeated address template --> 
    <h4>Student #{{i + 1}} 
     <input type="checkbox" formControlName="checked"> 
    </h4> 

此代码的工作

感谢您的帮助。

0

试试这个绑定,使用ngClass

<div [ngClass]="{'active':student.checked}">...</div> 

在div标签把学生,你有strudent,改变它

+0

不幸的是,它不起作用,它的行为与[class.active] =“student.checked”的行为一样,我犯了一个错误,简化了代码中的示例我有[class.active] =“student.checked”,我也试过

...

+0

你有导入CommonModule – alehn96

+0

你有div和在复选框学生strudentdent,那就是问题 – alehn96