2017-08-12 148 views
0

我想獲取並設置一個布爾值爲'checkvalue'。我想事先調用函數isItTrue來設置變量'checkvalue'。如果該框被(取消)選中/並且我檢查它,它應該調用函數doSomething()。 我正在試驗<input type="checkbox" id="{{ class.id }}" ng-init="checkBoxValue=isItTrue(class.id)"[(ngModel)]="class['checkBoxValue']"/>,但它似乎沒有工作。有什麼建議麼?Angular 4 - 獲取和設置模型

<table> 
     <thead> 
     <tr> 
     <th>class</th> 
     <th>subjects</th> 
     <th>class speaker</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr *ngFor="let class of classes"> 
     <td>{{ class.name }}</td> 
     <td> 
      <div class="form"> 
      <input type="text" class="md-input-text" placeholder=" " value=" " [(ngModel)]="subject"/> 
      <label class="md-desc">subjects</label> 
      </div> 
     </td> 
     <td> 
      <div class="md-checkbox"> 
      <input type="checkbox" id="{{ class.id }}" [(ngModel)]="class['checkBoxValue']"/> 
      <label for="{{ class.id }}"></label> 
      </div> 
     </td> 
     </tr> 
     </tbody> 
    </table> 
+0

ng-init無論如何都不是Angular的東西:) – Vega

回答

0

要做些什麼的組件初始化:

import { OnInit } from '@angular/core'; 

,讓你的組件實現它像

export class App implements OnInit{ 
} 

,你可以從ngOnInit生命週期的鉤子方法調用你的方法:

ngOnInit() { 
    isItTrue(); 
} 

設置一個布爾值,校驗值: 你應該有一個組件變量,並將其與綁定到輸入:

[(ngModel)]="checkvalue" 

要打電話給你的功能,當輸入的變化,您可以用變化實現它事件:

(change)="doSomething()" 

在此功能中,您可以用this.checkvalue您已經綁定訪問輸入的值。

查看全部代碼此planker。希望能幫助到你 :)。