2017-06-21 68 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