1

I have the following code for my component:

  export class OrderDetailsComponent implements OnInit {

  orderItem: OrderItem[];

  constructor(private route: ActivatedRoute,
          private httpClient: HttpClient) { }
  
  private orderUrl = 'http://localhost:8080/api/orders';

  ngOnInit(): void {
       this.route.paramMap.subscribe(() => {
  const theOrderId: number = +this.route.snapshot.paramMap.get('id')!;

  this.httpClient
    .get(
      `${this.orderUrl}/${theOrderId}/orderItems`,
      {
        responseType: 'json',
      }
    )
    .subscribe((response: any) => {
      var responseData = response;
      responseData.forEach((array: any) => {
        this.orderItem.push({
          imageUrl: array.imageUrl,
          unitPrice: array.unitPrice,
          quantity: array.quantity,
          productId: array.productId,
          producerId: array.producerId,
        });
      });
    });
})}}

I am getting order items of my order. And want to put them into the table on my HTML:

   <table class="table table-bordered" >
                <tr>
                    <th></th>
                    <th>Price</th>
                    <th>Qty</th>
                    <th>Producer</th>
                </tr>

                <tr *ngFor="let tempOrderItem of orderItem">
                    <td>
                            {{ tempOrderItem.imageUrl }}
                    </td>
                    <td>
                        {{ tempOrderItem.unitPrice}}
                    </td>
                    <td>
                        {{ tempOrderItem.quantity}}
                    </td>
                    <td>
                        {{ tempOrderItem.producerId }}
                    </td>                  
                    
                </tr>
            </table>

But when I run the app, I am getting only the header of my table. From BE all info comes ok, e.g.:

http://localhost:8080/api/orders/3/orderItems response

I was reading many questions here, I am assuming it is something related to the async,but I was trying some solutions that did not help me (I was trying to add into the html ngIf orderItems$ | async, tried to use not subscribe, but pipe(map(...).

What am I doing wrong here?

1

2 Answers 2

0

Angular updates the view only when it needs to, but as you are pushing the values in the array, it is still referencing the same value. Arrays, like every other object in JavaScript, are references to a point in memory unlike the primitives. So you have a couple of ways to let Angular know you need to update the view at this point. The simplest way would be to do something like this -

this.orderItem = responseData.map(array => ({
      imageUrl: array.imageUrl,
      unitPrice: array.unitPrice,
      quantity: array.quantity,
      productId: array.productId,
      producerId: array.producerId,
    });

As the map function returns a new array, this would tell Angular that something has to be updated in the view.

Another more advanced way to solve this would be to use markForCheck or detectChanges method of the ChangeDetectorRef though these are preferred when some non Default change detection strategy is used.

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

Comments

0

Many thanks to all answers. I found an issue. It is not an async related. In my example I had to add also:

right sol

After that the table loads Ok.

1 Comment

Quick note on images of text/code: they don't work so well with screen-readers, clipboards, or search-engine robots. Please use block formatting for text, unless an image is the only way the information can be conveyed.

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.