简体   繁体   English

Ionic2 ngFor视图未更新

[英]Ionic2 ngFor in view not updating

I have list of elements in my app, printing them out within *ngFor. 我的应用程序中有元素列表,可以在* ngFor中将它们打印出来。 When the app is resumed I call a method to load new elements from the server by doing a http-Request. 恢复应用程序后,我会通过执行http-Request调用一个方法从服务器加载新元素。 Inside this method I set the items as elements of the array. 在此方法内部,我将项目设置为数组的元素。

However the array is not updated in the view. 但是,数组不会在视图中更新。 If i do a pull-to-refresh (on which I call the same method) the elements in the view are updated. 如果我执行“强制刷新”(我在其中调用相同的方法),则视图中的元素将更新。

I tried wrapping the part of my code, that overrides the the old array in ngZone's .run() or a setTimeout() but nothing worked. 我尝试包装我的代码部分,该部分覆盖ngZone的.run()setTimeout()的旧数组,但没有任何效果。 Also tried with ChangeDetectorRef , also without success. 还尝试了ChangeDetectorRef ,也没有成功。

Now i am out of any ideas... 现在我没有任何想法...

Here is what I have tried so far: 到目前为止,这是我尝试过的:

this.zone.run(() =>{
   this.posts = posts; // "posts" is the array of elements i got from http-request
                       // "this.posts" the array for the view
});

and also 并且

setTimeout(() => {
   this.posts = posts;
}, 10);

and

this.posts = posts;
this.changedetectorRef.detectChanges(); //also tried markForCheck()

MY CODE: 我的密码:

view: 视图:

<ion-item-sliding *ngFor="let post of posts" >
    <ion-item>
        <p>{{post.newdistance}}</p>
    </ion-item>
</ion-item-sliding>

corresponding .ts file: 对应的.ts文件:

loadPosts() {
  let loading = this.loadingController.create({
    content: "Einen Moment bitte...",
    spinner: "crescent"
  });

  loading.present();

  this.postsService.getPostsFromServer().then(posts => {

    posts.forEach((post, index) => {

        this.geolocationService.getDistance(post.latitude, post.longitude).then(distance => {
            post.newdistance = distance;
        });

    });

    this.posts = posts; // <---- what to do here ???
    loading.dismiss();

  })
  .catch(() => {
    loading.dismiss();
  });
}

PLEASE help me! 请帮我!

UPDATE 更新

When i put an alert and compare the old post value with the new one, both are updated. 当我发出警报并将旧的发布值与新的发布值进行比较时,两者都会更新。 The problem is definitely the not-updating view 问题绝对是没有更新的观点

UPDATE 2 更新2

I noticed, when I add a button to the view on which i call the same function, the view is updated also...it just isn't updated when i resume the app... 我注意到,当我在视图上添加一个按钮并调用相同的功能时,该视图也会被更新...当我恢复该应用程序时它不会被更新...

The this.posts = posts; this.posts = posts; part of your code is executed possibly before the 您的部分代码可能在

this.geolocationService.getDistance(post.latitude, post.longitude).then(distance => {
            post.newdistance = distance;
        });

I say " possibly before ", because it is a promise, you cannot be sure when exactly it will return. 我说“ 可能在之前 ,因为这是一个承诺,您不能确定它何时会确切返回。 So the newdistance value is set after you assign the posts to this.posts . 因此, posts分配给this.posts 之后 ,将设置newdistance值。

The best way to dealing with this is To chain your promise methods to be executed one after the other, not in parallel. 解决此问题的最好方法是将您的promise方法链接在一起,而不是并行执行。

Another (more simple way) is to push each post into this.posts only after it has gotten the newdistance value. 另一种(更简单的方法)是仅在获得newdistance值之后才将每个post都推送到this.posts Keep in mind that you should use a new array to push the posts, like this: 请记住 ,您应该使用新数组来推送帖子,如下所示:

  //at the top of you page declare a new postsArray variable
  postsArray = [];
  this.postsService.getPostsFromServer().then(posts => {

    posts.forEach((post, index) => {

        this.geolocationService.getDistance(post.latitude, post.longitude).then(distance => {
            post.newdistance = distance;
            // Here we push the new post into the posts array
            this.postsArray.push(post);
        });

    });

    loading.dismiss();

  })
  .catch(() => {
    loading.dismiss();
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM