1

我有一個模型,它是一個數組元素。我想清除ngIf condition.Please每個元素的值在下面找到HTML:在angular2中重置ngIf的ngModel值

  <div *ngIf="flag" > 
      <table id="table" class="table table-hover table-bordered table-mc-light-blue"> 
       <thead> 
        <tr> 
         <th>col 1</th> 
         <th>col 2</th> 
        </tr> 
       </thead> 
       <tr *ngFor="let item of collection;"> 
        <td>{{item.col1}}</td> 
        <td> 
         <input type="text" class="form-control" [(ngModel)]="item.col2" #input="ngModel" name="input-{{i}}"> 
        </td> 
       </tr> 
      </table> 
     </div> 

標誌設置爲false,我想清楚了收集的所有值。 有一個選項初始化集合,但我不想這樣做,因爲我有幾個這樣的集合。

任何幫助將是可觀的!

+0

什麼時候'flag'的值會改變什麼事? – Aravind

+0

實際上,沒有事件,我將該標誌變量綁定到單選按鈕上作爲ngmodel。關於單選按鈕的更改,我正在顯示內容 –

+0

謝謝。 。 。 ..。 – Aravind

回答

1
<input type="radio" (change)="resetForm()"/> 


resetForm(){ 
    if(!this.flag){ 
      this.collection = new Array() 
    } 
} 
0

導致標誌被設置爲false的「事件」或方法調用是什麼?

這是您要清除集合的方法調用。

事情是這樣的:

public toggleFlag() { 
    this.expandFlag = !this.expandFlag; 
    this.collection = []; 
} 
+0

實際上,沒有事件,我把那個** flag **變量綁定到單選按鈕上作爲ngmodel。關於單選按鈕的變化,我顯示的內容 –

0

在您的組件:

ngDoCheck() { 
    if (!this.flag) { 
     this.collection = []; 
    } 
} 

而且你要實現DoCheck這樣的:

export class myClass implements OnInit, DoCheck { 

基本上,這是引發聽衆在發生的每一次變化中,我們檢查this.flag是否爲假我們清空collection數組元素。

+0

它的工作,但ngDoCheck被調用多次,所以我選擇了(更改)事件 –

+0

@JayakrishnanGounder哦,好的。我很高興,無論如何幫助...;)祝你有美好的一天! – SrAxi