NativeScript ListView 没有更新
NativeScript ListView isn't updating
所以我有一个像这样的 ListView
<ListView height="150" [items]="countries">
<ng-template let-country="item">
<Image src={{country.imgSrc}}></Image>
<Label text={{country.name}}></Label>
</ng-template>
</ListView>
我订阅了一个返回国家数组的 Observable。
countries: any[] = [];
ngOnInit() {
this.countryService.countriesUpdated.subscribe(
(countries:any[]) => {
this.countries = countries;
{
)
}
国家更新时。 ListView 不会使用新的 ListItem 更新屏幕。相反,如果我用一个值初始化国家/地区,它会显示 ListItem。我在屏幕上放置了一个带有点击侦听器的按钮,即 console.log(this.countries)。点击后,新的 ListItem 终于出现了。国家与 ListView 的绑定不应该自动更新吗?
本机脚本 6.0
Android 10
通常在这种情况下,您需要调用 refresh()。我不使用 angular,在 javascript 中,这类似于
page.getViewById("listview").refresh();
并有一个 id=listview,例如
<ListView id-"listview" height="150" [items]="countries">
您似乎正在使用 RxObservable 订阅 来填充您的项目。这意味着您需要使用 async pipe. For example, see here the async pipe is used here for the list's items property and how in the code behind you are populating the items in the subscription (here and here).
我将私有区域 NgZone
添加到我的构造函数中,然后像这样包装订阅响应:
this.zone.run(()=>{
me.countries= countries;
});
来源:https://discourse.nativescript.org/t/listview-does-not-refresh-update-when-a-new-value-is-added/1492
所以我有一个像这样的 ListView
<ListView height="150" [items]="countries">
<ng-template let-country="item">
<Image src={{country.imgSrc}}></Image>
<Label text={{country.name}}></Label>
</ng-template>
</ListView>
我订阅了一个返回国家数组的 Observable。
countries: any[] = [];
ngOnInit() {
this.countryService.countriesUpdated.subscribe(
(countries:any[]) => {
this.countries = countries;
{
)
}
国家更新时。 ListView 不会使用新的 ListItem 更新屏幕。相反,如果我用一个值初始化国家/地区,它会显示 ListItem。我在屏幕上放置了一个带有点击侦听器的按钮,即 console.log(this.countries)。点击后,新的 ListItem 终于出现了。国家与 ListView 的绑定不应该自动更新吗?
本机脚本 6.0 Android 10
通常在这种情况下,您需要调用 refresh()。我不使用 angular,在 javascript 中,这类似于
page.getViewById("listview").refresh();
并有一个 id=listview,例如
<ListView id-"listview" height="150" [items]="countries">
您似乎正在使用 RxObservable 订阅 来填充您的项目。这意味着您需要使用 async pipe. For example, see here the async pipe is used here for the list's items property and how in the code behind you are populating the items in the subscription (here and here).
我将私有区域 NgZone
添加到我的构造函数中,然后像这样包装订阅响应:
this.zone.run(()=>{
me.countries= countries;
});
来源:https://discourse.nativescript.org/t/listview-does-not-refresh-update-when-a-new-value-is-added/1492