【问题标题】:Ionic view not updating page when opened second time离子视图在第二次打开时不更新页面
【发布时间】:2017-05-10 14:38:51
【问题描述】:

我正在使用 ionic 2。我在 ionic 2 中面临很多问题。当我导航推送以显示数据时。第一次它工作正常。之后它在控制台中打印数据。但没有显示在屏幕上。

而且在终端中,当我对 .ts、.html、.scss 文件进行一些更改时。它没有更新。我需要关闭我的终端,然后我需要再次执行ionic serve --lab.. 那么是否将.ts 文件更改为.js。如果是,我需要更改新 .js 文件中的任何内容的语法吗?

谢谢, 萨蒂什

更新:

我有主屏幕,通过推送我传递 cat id 以根据我从 home.html 传递的猫 ID 在我的 settings.html 中填充数据:

我的home.html

  <div class="item item-body no-padding" style="border-width: 0px !important;">  

  <div class="row no-padding"  *ngFor="let data of Catdata;let i = index" (click)="showDetails(Catdata[i].CatID)">
     <ng-container *ngIf=" i % 2 === 0">

          <div class="col col-50 custom-design2" style="background: url(img url) no-repeat center;background-size: cover;">
               <div class="custom-design1"><span class="grid-title">{{Catdata[i].CategoryName}}</span></div>
            </div>

            <div class="col col-50 custom-design2" style="background: url(img url) no-repeat center;background-size: cover;">
                 <div class="custom-design1"><span class="grid-title">{{Catdata[i+1].CategoryName}}</span></div>
             </div>


     </ng-container>
</div> 

   
</div>

我的家.ts:

data: any;
 
   Catdata: any;

    constructor(   public alertCtrl: AlertController,
        public modalCtrl: ModalController,
        public navCtrl: NavController,
        public http:Http,
        public authService: AuthService) {
    this.submit();
      }
     submit() {     
     
        this.authService.categ(this.loginData).then((result) => {
        
          this.data = result;
    
           if(this.data.status == 1)
           {
           this.Catdata = this.data.CatgeoryList;
    
           
               for(let i=0; i<this.Catdata.length; i++) {
                   console.log(this.Catdata[i].CategoryName);
               }
    
           }
    
           else if(this.data.status == 0) {
    
         let alert = this.alertCtrl.create({
        title: 'Error',
        subTitle: 'Please Enter Valid Username & Password',
        buttons: ['OK']
      });
      alert.present();
           }
    
        }, (err) => {
        
        
        });     
      } 
    
    
    public showDetails(catId: any): void {
        this.navCtrl.push(SettingsPage, { clickedcatId: catId });
    
    
    }

我的设置.html:

<div class="item item-body no-padding" style="border-width: 0px !important;">  

  <div class="row no-padding"  *ngFor="let data of Catdata;let i = index" (click)="opndetailpage()">
     <ng-container *ngIf=" i % 2 === 0">

          <div class="col col-50 custom-design2" style="background: url(img url) no-repeat center;background-size: cover;">
               <div class="custom-design1"><span class="grid-title">{{data[i].SubCategoryName}}</span></div>
            </div>

            <div class="col col-50 custom-design2" style="background: url(img url) no-repeat center;background-size: cover;">
                 <div class="custom-design1"><span class="grid-title">{{data[i+1].SubCategoryName}}</span></div>
             </div>


     </ng-container>
</div> 

   
</div>

我的settings.ts

 data: any;
 
   Catdata: any;

    constructor( public alertCtrl: AlertController,
        public modalCtrl: ModalController,
        public navCtrl: NavController,
        public http:Http,
        public authService: AuthService,private navParams: NavParams) {
    
        this.categoryid = navParams.get('clickedcatId');
        console.log(this.categoryid);
    
    this.another();
    
    
      }
    
      another() {
    
       this.subcatdata = { CatID:this.categoryid};
      
      this.authService.subcatte(this.subcatdata).then((result) => {
        
          this.data = result;
    
          console.log (this.data);
    
           if(this.data.status == 1)
           {
           this.Catdata = this.data.SubCatgeoryList;
    
           
               for(let i=0; i<this.Catdata.length; i++) {
                   console.log(this.Catdata[i].SubCategoryName);
               }
    
           }
    
           else if(this.data.status == 0) {
    
         let alert = this.alertCtrl.create({
        title: 'Error',
        subTitle: 'Please Enter Valid Username & Password',
        buttons: ['OK']
      });
      alert.present();
           }
    
        }, (err) => {
        
        
        });     
    }

我的authservice.ts

这很好:

categ(cat: any) {
    return new Promise((resolve, reject) => {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        this.http.post(apiUrl+'categories.php', JSON.stringify(cat), {headers: headers})
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err);
          });
    });
  }

   subcatte(subcat: any) {
    return new Promise((resolve, reject) => {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        this.http.post(apiUrl+'subcategory.php', JSON.stringify(subcat), {headers: headers})
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err);
          });
    });
  }
  

【问题讨论】:

  • 我可以将 ionic2 .ts 文件更改为 .js 否 .. ionic 2 仅适用于 typescript。 当我做一些改变时。它没有更新 ..这是一个不同的问题。
  • ionic 会在您对任何文件进行更改后立即自动更新视图,这可能是因为您正在更改文件并保存,然后再次更改文件并保存,然后才能构建以前的保存状态。为了克服这个问题,您可能想尝试只输入空行并保存并让它构建应用程序的这种状态
  • @suraj 我发布了我的完整代码。我做错了什么。真的过去2天。我无法得到解决方案
  • @warl0ck 我发布了我的完整代码。我做错了什么。真的过去2天。我无法得到解决方案
  • 你能告诉它什么时候没有更新数据/用户界面你是否在它还在构建应用程序时进行了更改并保存了?

标签: typescript ionic-framework ionic2


【解决方案1】:

您的代码中的一切都很好,只是您忘记在 ionic 中处理 生命周期事件

问题是当你第一次加载页面时,它的构造函数被调用,这个页面被缓存,下一次没有调用构造函数,因此没有从服务器获取数据并在你的应用程序上显示空白页面。

这是更新后的代码,无论您是第一次访问该页面还是在 ionViewDidEnter() 函数中调用它之后,您的 another() 方法将始终被调用 正如文档中提到的ionViewDidEnter() 方法和生命周期事件部分中提到的here

ionViewDidEnter : void Runs when the page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page.

data: any;

Catdata: any;

constructor(public alertCtrl: AlertController,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public http:Http,
    public authService: AuthService, private navParams: NavParams) {

    this.categoryid = navParams.get('clickedcatId');
    console.log(this.categoryid);
}

ionViewDidEnter() {
    this.another();
}

another() {

    this.subcatdata = { CatID: this.categoryid };

    this.authService.subcatte(this.subcatdata).then((result) => {

        this.data = result;

        console.log(this.data);

        if (this.data.status == 1) {
            this.Catdata = this.data.SubCatgeoryList;


            for (let i = 0; i < this.Catdata.length; i++) {
                console.log(this.Catdata[i].SubCategoryName);
            }

        }

        else if (this.data.status == 0) {

            let alert = this.alertCtrl.create({
                title: 'Error',
                subTitle: 'Please Enter Valid Username & Password',
                buttons: ['OK']
            });
            alert.present();
        }

    }, (err) => {


    });
}

这里是更新的 settings.ts 代码,加载程序显示,直到从服务器获取数据:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { AlertController, ModalController, NavParams, LoadingController } from 'ionic-angular';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { AuthService } from '../../providers/AuthService';

@Component({
    selector: 'page-settings',
    templateUrl: 'settings.html'
})
export class SettingsPage {

    data: any;
    responsedata: any;
    Catdata: any = null;
    Catdatanames: any;
    categoryid: any;
    subcatdata: any;



    constructor(public alertCtrl: AlertController,
        public modalCtrl: ModalController,
        public navCtrl: NavController,
        public http: Http,
        public authService: AuthService, private navParams: NavParams,
        public loadingCtrl: LoadingController) {
    }

    ionViewDidEnter() {
        this.categoryid = this.navParams.get('clickedcatId');
        console.log(this.categoryid);
        this.presentLoadingText();
    }

    presentLoadingText() {
        let loading = this.loadingCtrl.create({
            spinner: 'hide',
            content: 'Please Wait...'
        });

        loading.present();
        this.another(loading);
    }

    another(loading) {

        this.subcatdata = { CatID: this.categoryid };

        this.authService.subcatte(this.subcatdata).then((result) => {

            this.data = result;

            console.log(this.data);

            if (this.data.status == 1) {
                this.Catdata = this.data.SubCatgeoryList;


                for (let i = 0; i < this.Catdata.length; i++) {
                    console.log(this.Catdata[i].SubCategoryName);
                }
            }

            else if (this.data.status == 0) {

                let alert = this.alertCtrl.create({
                    title: 'Error',
                    subTitle: 'Please Enter Valid Username & Password',
                    buttons: ['OK']
                });
                alert.present();
            }
            loading.dismiss();            

        }, (err) => {
            loading.dismiss();
        });
    }
    opndetailpage() {

    }

}

并更新了settings.html页面

    <ion-header>
  <ion-navbar color="navcolr" no-border-bottom>

  <!--   <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button> -->

    <ion-title>sub category</ion-title>
  <!--       <ion-buttons end>
      <button ion-button icon-only (click)="presentFilter()">
        <ion-icon ios="ios-options-outline" md="md-options"></ion-icon>
      </button>
    </ion-buttons> -->

  </ion-navbar>
</ion-header>

<ion-content style="width: 100%;overflow-y: hidden;">

<!-- <div style="
    margin-top: 3%;
    font-size: 15px;
    font-weight: 400;
    border-bottom: 1px #696969 solid;
    padding-left: 5%;
    padding-bottom: 3%;">
  <label>Resources</label>

</div>  -->

<p>
  This is sub cat
</p>

<div class="item item-body no-padding" style="border-width: 0px !important;">  

  <div class="row no-padding" *ngFor="let data of Catdata; let i = index" (click)="opndetailpage()">

          <div class="col col-50 custom-design2" style="background: url(Your_URL) no-repeat center;background-size: cover;">
               <div class="custom-design1"><span class="grid-title">{{Catdata[i].SubCategoryName}}</span></div>
            </div>
</div> 


</div>

<p>
  This is subjetcs
</p>

</ion-content>

请注意,我已经删除了其他 div 元素,因为当您在 Catdata 中的项目数为 5 时,它将无法获取 Catdata[5],因为只有索引 4 之前的元素。

如果您希望在一行中有 2 个项目,我建议您检查 ionic Grid Documentation,这正是您想要实现的目标。

这里是完整的:src

【讨论】:

  • 哦,我真的很困惑负载。给我一分钟。我将发布我正在做的确切代码。
  • 现在请查看我的帖子。我发布了我在项目中使用的真实代码。我已经像你说的那样改变了。但只有同样的错误。或者我又错过了一些东西或什么??
  • 现在很清楚了。请告诉我我做错了什么
  • 现在更清楚了,只是为了确认您在&lt;ng-container *ngIf=" i % 2 === 0"&gt; 中的代码不起作用?
  • 我把两列放在单行中。那也行不通。当我第一次推送导航时,更多。在我的设置页面中显示数据。当我再次这样做时,它没有显示数据
猜你喜欢
  • 1970-01-01
  • 2020-10-04
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2017-04-03
相关资源
最近更新 更多