【问题标题】:ListView in tabview doesn't updatetabview 中的 ListView 不更新
【发布时间】:2018-10-13 04:42:06
【问题描述】:

您好,我在 TabView 中的 listView 有一个很奇怪的问题。我整天都在解决这个问题,但没有结果。由于没有刷新 listView(ngZone 没有帮助),我必须在 listView 中以编程方式刷新。它可以工作,但问题是当我通过路由器从另一个页面转到此页面时,然后通过构造函数上一页注入。 我有登录页面:

this.router.navigate(["/home"]);

在 home.component.ts 中的构造函数看起来像:

constructor(ngZone: NgZone, private page: Page) 

然后我尝试调用:

var listview:ListView= <ListView> page.getViewById("listView1");

但它返回未定义(如果登录页面不存在并且只有主页存在,则它可以正常工作)。当我尝试从登录页面获取一些视图时,我得到了视图......

谁能给我建议什么是错的?我尝试了很多我发现的东西,但没有任何效果...谢谢您的回答。

编辑: 添加代码sn-p: home.component.html

<GridLayout class="page">
    <TabView height="100%" (selectedIndexChange)="onTabChanged($event)" class="content p-20" >
        <ng-container *ngFor="let itema of objectKeys(site)" >
            <StackLayout *tabItem="{title: itema}">
                    <ListView [id]="itema" class="list-group" [items]="getData(itema)" (itemTap)="onItemTap($event)">
                        <ng-template let-item >
                            <GridLayout class="list-group-item" rows="*" columns="auto, *">
                                <Label row="0" col="0" [text]="item.name"></Label>
                                </Image>
                            </GridLayout>
                        </ng-template>
                    </ListView>
                </GridLayout>
            </StackLayout>
        </ng-container> 

    </TabView>
</GridLayout>

home.components.ts 部分:

   ngOnInit(): void {
            getJSON(url).then((r: any) => {
                console.log(r);
                this.ngZone.run(() => {
                    this.site = r;
                });
                //this part added because ngZone didn't work
                var listview: ListView = <ListView>this.page.getViewById(objectKeys(this.site)[0]);
                listview.refresh();
            }, (e) => {
                //TODO handle exception
                console.log(e);
            });
        }

我认为问题出现是因为 ListView 是动态构造的(刷新在 onTabChange 方法中也不起作用。)

【问题讨论】:

  • 您是否正在尝试就您的ListView 不更新或从您的HomeComponent 访问您的ListView 获得帮助?
  • 如果我能以不同于 listView.refresh() 的方式获得 ListView 更新,那将是最好的,但我尝试了我发现的所有内容(ngZone 等),而 listview.refresh() 只是其中的一个正确地...但是当我添加登录页面时,我无法通过页面访问列表视图,因为通过构造函数注入的页面(您可以在上面看到)从登录而不是从家中注入页面。我也试过 {frame.topmost().currentPage;}。
  • 你可以提供更多细节和代码sn-p吗?
  • 已添加。这真的是因为 ListView 的动态构造...当我尝试在静态 ListView 上执行时,它正在工作。
  • 您可以尝试排除ng-container,看看是否有帮助。

标签: angular listview nativescript angular2-nativescript nativescript-angular


【解决方案1】:

我没有发现动态选项卡/ng-container 有任何问题。

这是一个Playground sample,已在 iOS 和 Android 上进行了测试。如果您仍有问题,请更新 Playground 示例,我们可以在其中重现您的问题。

【讨论】:

  • 非常感谢您的回答。在调查了这个问题的另一天后,我发现核心问题不在 ListView 中,而是在 TabView 中。我在 [this](stackoverflow.com/questions/52783746/… 问题中描述它。我修改了你的示例here。
  • 只需尝试单击我在上面链接的修改示例中的列表项,您将看到...(在 android 上测试)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
相关资源
最近更新 更多