0

I need to update an HTML table after updating my values. On Ionic (based on Angular) all was working fine when I was calling my datas loading function. But in Angular, whenever I call my data loading function, the array isn't updated..

I've already tried to deal with ChangeDetectorRef but nothing works...

Here are my functions :

loadCat() {
    this.apiService.listCat().then(data => {
      this.categories = data;
    });
    this.ref.detectChanges();
  }

  addCat() {
    this.apiService.createAssoc(this.label);
    this.assoForm.reset();
    this.loadAssoc();
  }

  deleteCat(id) {
    this.apiService.deleteCat(id);
    this.notify.fireSuccess("La catégorie a bien été supprimée !", "Bye bye !");
    this.loadCat();
  }

And here is a part of HTML table code :

 <tbody>
                        <tr *ngFor="let cat of categories; index as i">
                            <th scope="row">{{ i+1 }}</th>
                            <td>{{ cat.label }}</td>
                            <td><button (click)="deleteCat(cat._id)" class="btn btn-danger">Supprimer</button></td>
                        </tr>
                    </tbody>

I need by tags to be reload with my new categories values but nothing works...

5
  • Where is your deleteCat(cat._id) function . Commented Sep 13, 2019 at 19:57
  • you use categories in your template but i can't find where you update this variable in your component Commented Sep 13, 2019 at 20:03
  • Sorry, updated it (wrong component) Commented Sep 13, 2019 at 20:06
  • have you tried console.log(data) inside then? Commented Sep 13, 2019 at 20:14
  • @AndriyLozynskiy, looks like it refresh with a console.log (only with inspect tab opened)... but why ? Commented Sep 14, 2019 at 12:21

2 Answers 2

3

You must be use this.ref.markForCheck();

Sign up to request clarification or add additional context in comments.

Comments

0

No point in calling change detection actually before receiving data. You should move change detection to then block because it's called asynchronously after loadAssoc has been called.

Sometimes change detection should be wrapped with setTimeout because variable perhaps hasn't been updated in time. Set timeout will trigger change detection after call stack has been cleared which will guarantee that variable had been updated.

loadAssoc() {
    this.apiService.listAssoc().then(data => {
      this.assoc = data;
      setTimeout(() => this.ref.detectChanges());
    });
  }

4 Comments

Actually, you don't need to use 'setTimeout' here, just call 'detectChanges()' right after assigning 'data' to 'this.assoc'.
I agree with you. I added that just in case.
Actually, not working in my case... Nothing reloading when calling the function..
Are you sure that 'then' block is being called? Perhaps you haven't received the response. Also i guess you know this but change detection ref works only when you are using on push change detection strategy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.