0

I'm passing a user object into sessionStorage. When the next page is showing the string interpolation is not working. Even though the data is passed and the variable is filled.

export class ShoppingComponent implements OnInit, OnDestroy {
user: User;
private products: Product[] = [];
private cart: string[] = [];
private subscription: Subscription;

constructor(private shoppingService: ServiceShopping) { }

ngOnInit() {
// this.cart = this.service.getCart()
console.log("User: ", this.user);
this.subscription = this.shoppingService.getAllProducts().subscribe(
  (products: Product[]) => {
    console.log(products);
    this.products = products
    this.user = JSON.parse(sessionStorage.getItem('user'));
  }
 )
 }

  ngOnDestroy() {
  this.subscription.unsubscribe()
 }

 }

here is the html:

<h4>Hello {{user.name}}</h4>
<hr>
<div class="row">
        <app-shopping-item 
        *ngFor="let product of products"
        [product]="product"></app-shopping-item>
</div>

the {{user.name}} is not updating with the data

Narm
  • 10,677
  • 5
  • 41
  • 54
  • This sounds like it may be an issue with Angular change detection. [This post](https://stackoverflow.com/questions/50519200/angular-6-view-is-not-updated-after-changing-a-variable-within-subscribe) may help. – Narm Sep 20 '19 at 21:05
  • what do you mean by "When the next page is showing" please add the sample code what you have done to stackblitz then others can easily answer you – Indrakumara Sep 21 '19 at 03:06

0 Answers0