1

I am stuck with the following: I have a parent component in Angular 5 that displays childcomponents for each element in my cartProducts array. My childcomponents receive a cartProduct object as input. My cartProducts are received from a BehaviorSubject object. Whenever I receive updates, I update my array. I expected that all my childcomponents would react on updates to my array, but this does not happen. What am I doing wrong?

parent.component

constructor(private service:Service) {
 this.service.cpBehaviorSubject.subscribe({next: (v) => {
 console.log('Change triggerend in parent.');
 this.cartProducts = v;
 }});
}

parent.html

 <ng-template ngFor let-cp [ngForOf]="this.cartProducts">
 <app-child [cartProduct]="cp"></app-child>
 </ng-template>

child.component

 ngOnChanges() {
 console.log('Change triggered in child!');
 }
asked Apr 15, 2018 at 16:39
2
  • I've never worked with Angular, but try to update the content of cartProducts rather than replace the reference to it with v... Commented Apr 15, 2018 at 16:42
  • 1
    Does the list returned by cpBehaviorSubject keep referring to the same cart product instances on each update, or does it return new instances? I suspect that it returns the same instances, and that this is why Angular does not detect any change Commented Apr 15, 2018 at 17:02

4 Answers 4

0

Change cartProducts to receive BehaviorSubject instead and then use async pipe in the template.

cartProducts$; 
constructor(private service:Service) {
 this.cartProducts$ = this.service.cpBehaviorSubject();
}
<ng-template ngFor let-cp [ngForOf]="this.cartProducts$ | async">
 <app-child [cartProduct]="cp"></app-child>
 </ng-template>
answered Apr 15, 2018 at 16:51
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try this in your parent component:

constructor(private service:Service) {
 this.service.cpBehaviorSubject.subscribe((v) => {
 console.log('Change triggerend in parent.');
 this.cartProducts = v;
 });
}
answered Apr 15, 2018 at 16:52

Comments

0

Angular does not do change detection on the contents of your array, just on the array reference.

The simplest way to trigger the change detection is to update the array reference after updating the contents of the array with array.slice().

So, for example,

myArray.push(foo);
myArray = myArray.slice();
answered Apr 15, 2018 at 19:19

Comments

0

What about <ng-template> ? Accourding to the deffinition, a ng-template will not be rendered on the DOM. Clicking to the link below you will be redirected to TekTutorialsHub where you will understand how ng-template work.

https://www.tektutorialshub.com/angular/ng-template-in-angular/

Due to the fact that it is not rendered, the ngOnChanges life cycle hook is not triggered

answered Mar 14, 2024 at 14:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.