Change detection issue in ListView
Conditional styling in ListView templates don't get consistently applied. Most times they require the item to go off screen and back in, in order for the new status to be reflected.
Gif

Here is a sample to reproduce:
Component:
import {Component} from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public options: string[] = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
public selectedOption: string = "";
public onItemTap(args) {
this.selectedOption = this.options[args.index];
}
}
Markup:
<StackLayout>
<ListView #listview [items]="options" separatorColor="white" (itemTap)="onItemTap($event)">
<template let-item="item" let-i="index" let-odd="odd" let-even="even">
<GridLayout columns="44, *" height="48">
<StackLayout colSpan="2" verticalAlignment="bottom" class="line"></StackLayout>
<Label col="1" [text]="item" style="font-size:20" [class.active]="selectedOption == item"></Label>
</GridLayout>
</template>
</ListView>
</StackLayout>
CSS
.active {
color:red;
}
It looks like a change detection issue. The list-view has OnPush change detection strategy (for performance reasons). Probably, thats why the items in it are note updated when selectedOption is changed.
One thing I noticed that if you use tap listener on the grid inside the template - it works as expected:
<GridLayout height="48" (tap)="selectedOption = item">
In this case, because the event is executed in the actual item - it triggers its change detector.
I ran into a very similar issue, but adding a (tap) wasn't possible in my case.
However, I did solve it by changing the instance variable type to BehaviorSubject, and using the async pipe in the template.
I am having the same issue, having a class toggled by an item's property (in a list-view). Is there a way to manually trigger change detector for an item? In my case the property is changed after a request to an API, so I can't do the change in the layout.
Even though your problem is a little bit strange, you could try this:
import { Component, NgZone } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public options: string[] = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
constructor(private zone: NgZone) { }
public selectedOption: string = "";
public onItemTap(args) {
this.zone.run(() => {
this.selectedOption = this.options[args.index];
});
}
}
Once I had a problem like that and I fixed it by running the code inside the NgZone.
Trying with NgZone didn't work for me.
With RadListView, changing an item's property updates immediately the UI, but since it has a memory leak (an issue was created on telerik/nativescript-ui-feedback) and kills my app after a while, I can't use it.
With ListView, the UI is not updated. Is there any alternative to NgZone that could work?
After trying some modifications to my code, I could solve this issue by doing:
...
this.items[i].is_list = true;
this.items = this.items.slice();
That way, when reassigning to this.items it's own array, the UI got updated.
Same problem. Thanks @eduardoturconi , that worked for me.
Worth noting: This isnt just on angular, I am experiencing this in a Typescript/vanilla app. I fix it by calling refresh() on the listview, though that shouldnt be necessary.
This happens only on iOS, works correctly on Android.
this is really a big problem on iOS, I've tried with NgZone.run, it did't work, also tried with refresh() also don't work at all.
but on android, I don't need NgZone nor refresh to make this kind of change work. it just work perfectly.
When could this be fixed?
Also experience this issue on IOS...seems like it doing the buggy thing when bind function and works perfectly when bind to property
Experiencing the same problem. The issue is specific to IOS for me as well.
Calling refresh() on the listview was the only suggestion to resolve the problem, though i'm hopeful that it is a temporary resolution to the bigger problem. Calling refresh() is causing the view to flash, which looks odd and makes for a poor UX. Hope this is still being looked into.
Flashing problem as described by @DillonStreator can be "solved" by overwriting the default behaviour as seen here
<ListView [items]="items" (itemLoading)="onItemLoading($event)">
onItemLoading(args: ItemEventData) {
const iosCell = args.ios;
iosCell.selectionStyle = UITableViewCellSelectionStyle.None;
}
Is there any possible option to fix this if the source collection doesn't change? IF i'm changing only property visibility="visible || collapsed" of the view of tapped ITEM . I've tried @eduardoturconi .slice() method and others like:
- listView = this.page.getViewById
("listView") listView.refresh(); - BehaviourSubject and BehaviourSubject with NgZone.run
- @Dagorwolf method; !!! Nothing work correctly Please, would appreciate any ideas.
I solved this problem as described here.
Just add this:
Object.defineProperty(View.prototype, "isLayoutValid", {
get: function () {
return (this._privateFlags & PFLAG_LAYOUT_REQUIRED) !== PFLAG_LAYOUT_REQUIRED;
},
enumerable: true,
configurable: true
});
in
node_modules/tns-core-modules/ui/core/view/view.ios.js
And everything was magically fixed!
Have the same issue, tried https://angular.io/api/core/ChangeDetectorRef but no success, tried ngZone and no success. Is it related to ListView? e.g If I change to a StackView with ScrollView around would it work?
onItemLoading() does not get triggered when coming backwards with router. Only forwards
@drmistral , if Im using angular and I would change view.ios.js, wouldn't it override this file on build?
@ShyshkovOleg, I'm not using angular but anyway I put view.ios.js under git control, just to detect any overwriting. Of course the best solution would be fix this problem at the root but meanwhile...
We're seeing this on Android when using the async pipe. A workaround we found was using the itemLoading event on ListView, and then do a markForCheck() inside a setTimeout.
So in template
<ListView [items]="data | promisePipe | async" (itemLoading)="refresh()"></ListView>
In TS file:
refresh() {
setTimeout(() => this.changeDetectorRef.markForCheck(), 0); // Queue macro event
}
Is there any news to this story? I'm having similar issues on IOS. I have tried so many different things now and nothing helps.