0

I have a view with the following snippet:

<div *ngIf="step" [sortablejs]="orderedId" [sortablejsOptions]="optionsSortable">
  <div *ngFor="let item of step.items" class="item">
    <div class="position">{{item.position}}</div>
    <div class="text">{{item.text}}</div>
  </div>
</div>

In controller, I have the following snippet:

export class AppComponent implements AfterViewInit {
  step: StepInterface;
  stepId: number;
  orderedId: Array<number>;

  constructor(private api: ApiService) {

    this.stepId = 1 //Debug

    this.optionsSortable = {
      onUpdate: (event: any) => {
        this.api.organize(this.stepId, this.orderedId).subscribe((response) => {
          this.step = response;
          this.buildArray();
        });
      }
    };
  }

  ngAfterViewInit() {
    this.api.get(this.stepId).subscribe((data) => {
      this.step = data;

      this.buildArray();
    });
  }

  private buildArray() {
    this.orderedId = [];
    for (const item of this.step.items) {
      this.orderedId.push(item.id);
      console.log(clue.id);
    }
  }
}

In fact, I have sortable div and when user modify the position, data is updated with backend. Problem is when the position is updated, so object "step" modified, {{item.position}} is not refreshed in view. How can I update the position in view after position modification?

Thank you

1 Answer 1

3

ChangeDetectorRef

if there is a case where any thing inside your component data has changed but it hasn't reflected the view, you might need to notify Angular to detect those changes ( detect local changes) and update the view.

import { Component, Input, ChangeDetectionStrategy,ChangeDetectorRef } from 
   '@angular/core';
@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})

export class AppComponent implements AfterViewInit {
  step: StepInterface;
  stepId: number;
  orderedId: Array<number>;

  constructor(private api: ApiService,private cdr:ChangeDetectorRef) {

    this.stepId = 1 //Debug

    this.optionsSortable = {
      onUpdate: (event: any) => {
        this.api.organize(this.stepId, this.orderedId).subscribe((response) => {
          this.step = response;
          this.buildArray();
        });
      }
    };
  }

  ngAfterViewInit() {
    this.api.get(this.stepId).subscribe((data) => {
      this.step = data;

      this.buildArray();
    });
  }

  private buildArray() {
    this.orderedId = [];
    for (const item of this.step.items) {
      this.orderedId.push(item.id);
      console.log(clue.id);
    }
     this.cd.detectChanges();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

the only change is: this.cd.detectChanges(); but I'm not sure what cd is.
I guess cd should be a member that takes on the value of the cdr param passed to the ctor.

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.