Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Reset ngModel values on ngIf in angular2

I have a model which is an array element. I want to clear the values of each element on ngIf condition.Please find below HTML :

         <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>

On flag set to false, I want to clear all values of the collection. There is an option of initializing collection, but I don't want to do that as I have several such collections.

Any help would be appreciable!!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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


resetForm(){
     if(!this.flag){
          this.collection = new Array()
     }
}
over 4 years ago · Santiago Trujillo Report

0

What is the "event" or method call that causes the flag to be set to false?

This is the method call where you'll also want to clear the collection.

Something like this:

public toggleFlag() {
  this.expandFlag = !this.expandFlag;
  this.collection = [];
}
over 4 years ago · Santiago Trujillo Report

0

In your component:

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

And you have to implement DoCheck like this:

export class myClass implements OnInit, DoCheck {

Basically, this is a listener that trigger at every change that happens, and here we check that this.flag is false and we empty the collection array element.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!