[英]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 的綁定不應該自動更新嗎?
NativeScript 6.0 安卓 10
我將私有區域NgZone
添加到我的構造函數中,然后像這樣包裝訂閱響應:
this.zone.run(()=>{
me.countries= countries;
});
來源: https : //discourse.nativescript.org/t/listview-does-not-refresh-update-when-a-new-value-is- added/ 1492
通常在這種情況下,您需要調用 refresh()。 我不使用 angular,在 javascript 中這會是這樣的,
page.getViewById("listview").refresh();
並有一個 id=listview 例如
<ListView id-"listview" height="150" [items]="countries">
看起來您正在使用RxObservable 訂閱來填充您的項目。 這意味着您需要使用async pipe 。 例如, 請參閱此處異步管道用於列表的items屬性以及您背后的代碼如何填充訂閱中的項目( 此處和此處)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.