3

I have a variable isLTEavailable in my ts file inside a function. When the function is called, based on the a condition, isLTEavailable's value changes and is logged correctly, but for some reason, it is not being updated in the DOM.

Here's my ngif condition:

<li class="nav-item dropdown" *ngIf="isLTEavailable">

Here's my ts function:

console.log("Lte --", lte_id)

if (lte_id != undefined) {
    sessionStorage.setItem("lte_customer_id", lte_id.toString())
    this.isLTEavailable = true;
    console.log("isLTEavailable -----> ", this.isLTEavailable)
} else {
    sessionStorage.setItem("lte_customer_id", 'n/a')
    this.isLTEavailable = false;
    console.log("isLTEavailable -----> ", this.isLTEavailable)    
}

I printed the variable using string interpolation as well and it always shows true as its value even though console updates correctly.

P.S. isLTEavailable is initialized as true.

uminder
  • 23,831
  • 5
  • 37
  • 72
Ujjwal Sharma
  • 96
  • 2
  • 11

4 Answers4

4

According to this

Change detection fails on change in a variable inside a callback or subscribe method. One could use

 ngZone.run(() => { this.isLTEavailable = false }) 

to update the value. (or) trigger the change detection manually by following

import {ChangeDetectorRef} from '@angular/core'
constructor(private ref: ChangeDetectorRef){}


//after variable update
this.ref.detectChanges();

Let me know if this works. Thanks

Mukundhan
  • 3,284
  • 23
  • 36
1

Make Use of Angular's Observable

In your TS file :

Declare :

  import { Observable } from 'rxjs';
  isLTEavailable : Observable<boolean> ; 

  //and in your function
  console.log("Lte --", lte_id)

  if (lte_id != undefined) {
    sessionStorage.setItem("lte_customer_id", lte_id.toString())
    this.isLTEavailable = new Observable(observer=>observer.next(true));
  } else {
    sessionStorage.setItem("lte_customer_id", 'n/a')
    this.isLTEavailable = new Observable(observer=>observer.next(false));
  }

Then in your HTML Use the ASYNC pipe :

 <li class="nav-item dropdown" *ngIf="isLTEavailable | async">

Here is the working example i have put together for you to look at :

https://stackblitz.com/edit/angular-v4dvx6

Wisely D Cruizer
  • 916
  • 9
  • 23
1

Thanks all for having a healthy discussion here. I found the issue with my code and fixed it and now the interpolation works as expected. The issue was the initialisation of isLTEavailable variable inside the ngOnInit which resulted it in being updated back to the original state whenever the function was called. Again, thanks for helping me out. I got to learn about the changeDetection function and learned about using Observables as an alternative from your answers. Thank you. Really appreciate it.

Ujjwal Sharma
  • 96
  • 2
  • 11
0

Maybe you can use ChangeDetectorRef https://angular.io/api/core/ChangeDetectorRef like this this._changeDetectorRef.detectChanges(),

constructor(private _changeDetectorRef: ChangeDetectorRef) {

}

//IN YOUR FUNCTION
if (lte_id != undefined) {
    sessionStorage.setItem("lte_customer_id", lte_id.toString())
    this.isLTEavailable = true;
    console.log("isLTEavailable -----> ", this.isLTEavailable)
    this._changeDetectorRef.detectChanges()

} else {
    sessionStorage.setItem("lte_customer_id", 'n/a')
    this.isLTEavailable = false;
    console.log("isLTEavailable -----> ", this.isLTEavailable)
    this._changeDetectorRef.detectChanges()   
}
Muhammet Can TONBUL
  • 3,217
  • 3
  • 25
  • 37