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

207
Views
ngModel values are not getting displayed in controls in angular2

I have a model which is an array element and it's corresponding HTML view is as below:

<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>
                   <th>col 3</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 some conditions, I am inserting new elements using splice.Please find below code

this.collection.splice(LastIndex, 0, ...newArray);

The problem is that after insertion the previous ngModel values are not getting displayed for certain records. I inspected the element and found ng-reflect-model set to previous values, but I couldn't see the values in input controls.

Please find the image html View

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I got it working by modifying my logic as below:

    let tempCollection = this.collection.slice();

    //empty the list       
    this.collection = [];

    this.changeDetectorRef.detectChanges(); //needed to reflect changes

    //insert elements in the list
    tempCollection.splice(LastIndex, 0, ...elements);

    //push once again to collection
    this.collection.push(...tempCollection);

Instead of directly manipulating the collection, I copied it into the temporary variable called tempCollection for manipulation and cleared the collection variable.

Once manipulation is done, I pushed entire tempCollection to collection. I used this.changeDetectorRef.detectChanges() to reflect the changes, once I had cleared collection.

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!