admin管理员组文章数量:1277393
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.
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.
- have you tried with isLTEavailable == true ?? – Mukundhan Commented Dec 19, 2019 at 7:18
- yes i did. Still the same issue – Ujjwal Sharma Commented Dec 19, 2019 at 7:23
- can you provide working demo, i need to see your condition and where are you calling the function – varundhariyal Commented Dec 19, 2019 at 7:24
- Where are you running this function? – manneJan89 Commented Dec 19, 2019 at 7:29
-
Just display the value using interpolation i.e.
{{isLTEavailable}}
above the list item and make sure the value is getting changed. – MonkeyScript Commented Dec 19, 2019 at 7:29
4 Answers
Reset to default 5According 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
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./edit/angular-v4dvx6
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.
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()
}
本文标签: javascriptngIf not updating DOMStack Overflow
版权声明:本文标题:javascript - ngIf not updating DOM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225480a2361779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论