1

In the following code, how can I make the orderItems auto refresh after I call addOrderItem()? Currently I have to call this.service.getOrderItems() again to manually refresh it.

export class OrderComponent implements OnInit {
    orderItems: OrderItem[];
    ...
    ngOnInit() {
        ...
        this.orderItems = this.service.getOrderItems();
    }
    addOrderItem(): void {
        this.service.addOrderItem();
    }
}
3
  • adding an item to a data bound array should already auto refresh without you needing to do anything Commented Oct 27, 2018 at 2:54
  • @pixelbits The order items array is returned by the service which I think is a deep copy of the original array. When I call the service to add things to the original array, this deep copy does not refresh but the original array does. Commented Oct 27, 2018 at 4:01
  • Ah,I understand they are different arrays. Commented Oct 27, 2018 at 4:03

1 Answer 1

1

You encapsulated your addOrderItem() service method so you can call getOrderItems in this encapsulation so it will be refreshed each time you add an OrderItem:

addOrderItem(): void {
  this.service.addOrderItem();
  this.orderItems = this.service.getOrderItems();
}

You can also subscribe to an event from your service to call a function each time addOrderItem() is called.

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

Comments

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.