0

i am using ionic 2.I am facing lots of problem in ionic 2. When i navigate push to show the data. At first time its works fine. After that its printing the data in console. But not showing in screen.

And also in terminal, when i do some changes in .ts,.html,.scss file.Its not updating . I need to close my terminal and again i need to do ionic serve --lab.. so is there any change the .ts file to .js. And if yes, does the syntax i need to change any thing inside new .js file.

Thanks, sathish

Update :

I have home screen and by push i am passing the cat id to pupulate the data in my settings.html based on the cat id that i am passing from home.html:

my 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>

my home.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 });
    
    
    }

My settings.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>

my 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) => {
        
        
        });     
    }

My authservice.ts

This is fine:

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);
          });
    });
  }
  
Community
  • 1
  • 1
venky
  • 139
  • 4
  • 12
  • _Can i change ionic2 .ts file to .js_ no .. ionic 2 only works with typescript. _when i do some changes.Its not updating_ .. that is a different issue. – Suraj Rao May 08 '17 at 10:42
  • ionic automatically updates the view as soon as you make changes to any of the files, this might be because you are changing your files and saving and then again changing file and saving before it could build the previous save state. to overcome this, you may want to try just put empty line and save and let it build this state of application – warl0ck May 08 '17 at 10:44
  • @suraj i posted my full code. what i am doing wrong. really for past 2 days. i was not able to get the solution – venky May 08 '17 at 10:47
  • @warl0ck i posted my full code. what i am doing wrong. really for past 2 days. i was not able to get the solution – venky May 08 '17 at 10:47
  • can you tell when it did not updated the data/UI did you made changes and saved when it was still building the app ? – warl0ck May 08 '17 at 10:51
  • @suraj even if any one want i can send my demp project, that making same issue like this – venky May 08 '17 at 10:51
  • @warl0ck even if any one want i can send my demp project, that making same issue like this – venky May 08 '17 at 10:51
  • @warl0ck no, i wont save the data or again build. at a first time it will show, same i will do it again with out save or build. but it wont show any data – venky May 08 '17 at 10:52
  • no need to write same comments addressing individually. you can address both in same comment – warl0ck May 08 '17 at 10:52
  • i think you are never equating the `result` you got from server to the variable you are looping for in your html code `Catdata` – warl0ck May 08 '17 at 10:59
  • @warl0ck sorry i din get you.. – venky May 08 '17 at 11:10
  • can i share u my demo project ...to you.. if you help me to sort this out. it will be much more helpfull – venky May 08 '17 at 11:11
  • in which screen and which api method you are taking about. ??.`this.data = result;` so i have all the response in `this.data`. And from that i am checking the status and saving the array in `Catdata`. And using for loop i am try to fetch the CategoryName. And i am try to display that name in html – venky May 08 '17 at 11:14
  • your detailpage.ts which renders that html code that you have posted – warl0ck May 08 '17 at 11:20
  • @warl0ck now what should i need t change ... ?? – venky May 08 '17 at 11:48
  • @warl0ck i am not understanding what you are trying to tell ...`you are never equating the result you got from server to the variable you got from server to the variable you are looping for in your html code Catdata` ? ` – venky May 08 '17 at 11:53
  • @suraj can you help me this post http://stackoverflow.com/questions/43883108/ionic-1-themeable-browser-not-opening-in-android-device – venky May 10 '17 at 05:01
  • @suraj if u give same solution in ionic 2 it will be much good.... here my project https://drive.google.com/file/d/0B97QeJKOjA1LWE94RXo3U3g1Ulk/view – venky May 10 '17 at 05:04
  • @venky you should form a proper question for it in SO if you want someone to help.. There may be others who could give a better sol than me as well.. Also very few people would be inclined to go through a project link.. – Suraj Rao May 10 '17 at 05:07
  • @suraj okay........ – venky May 10 '17 at 05:09
  • @warl0ck you there. i tried with ionic 2, ia m getting error for that browser plugin themeable browser..here my post http://stackoverflow.com/questions/43889322/ionic-2-themeable-browser-not-working-only-white-screen-in-android-device – venky May 10 '17 at 10:06

1 Answers1

1

Everything in your code is fine except you forgot to take care of lifecycle Events in ionic

what went wrong is when you first load the page it's constructor gets called, and this page gets cached and next times constructors is not called hence is data is not fetched from the server and shows empty page on your app.

Here is the updated code where your another() method will always be called either you are visiting the page for the first time or not after calling it in ionViewDidEnter() function as ionViewDidEnter() method as mentioned in the documentation and mentioned here in lifecycle events section

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) => {


    });
}

Here is updated settings.ts code with loader showing till the data is fetched from the server:

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() {

    }

}

And updated settings.html page

    <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>

Notice I have removed other div element as when you have odd number of items in Catdata in your case as 5 it wont be able to fetch Catdata[5] as there are only elements till index 4.

If you want 2 Items in a single row I recommend you check ionic Grid Documentation which is exactly what you want to achieve.

Here is the complete: src

warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • oh i really confused load. give me one min. I will post the exact code what i am doing. – venky May 08 '17 at 12:05
  • now please check my post. i posted my real code i am using in my project. i already changed like what you have said. But same error only. or again i am missing some things or what ?? – venky May 08 '17 at 12:10
  • now its very clear .please tell me what i am doing wrong inthis – venky May 08 '17 at 12:14
  • now It's more clear, just to confirm your code inside `` is not working right ? – warl0ck May 08 '17 at 12:15
  • that i put to diaply two col in single row. That also not working. more over first time when i do push navigation . in my settings page its showing data. Same time when i do it again its not showing the data – venky May 08 '17 at 12:16
  • second time as in when you visit the page again it does not show the data is that it ? – warl0ck May 08 '17 at 12:17
  • i did what you said, now same thing i am getting .here my demo source code...https://drive.google.com/file/d/0B97QeJKOjA1LbnhWWXN0UGFralU/view?usp=sharing – venky May 08 '17 at 12:38
  • please help me out. i dont know why i am getting like this. I have shared my project zip – venky May 08 '17 at 12:39
  • after putting you `another` method in `ionViewDidEnter()` it should have solved the problem if it rendering the values second time – warl0ck May 08 '17 at 12:40
  • yes, i put same.u can check my code. even now not data are sjhowing at first time itself. but i ma getting correct response data in my console. – venky May 08 '17 at 12:47
  • all your methods are `onViewDidEnter` instead of `ionViewDidEnter()` – warl0ck May 08 '17 at 13:06
  • bro, that i changed. After that does its work and when u navigate from home to setting page through push does its working ?? all time does its showing data ?? – venky May 08 '17 at 13:12
  • and how did u tell exactly that `` tis is not working ??...can you explain me why ? – venky May 08 '17 at 13:20
  • Sorry didn't get you, what exactly you are asking here, as in your Google drive when I change your method to correct method name i.e. ionviewdidenter it works fine for me. – warl0ck May 08 '17 at 13:23
  • and you asked me `` this making problem or not ??. actually i need to show 2 col per row.but this also not working – venky May 08 '17 at 13:25
  • i am trying the same thing. but the data is not showing in mine. ?? – venky May 08 '17 at 13:27
  • It's showing in home page at least ? for home page it's working fine for me, I'll try for settings page as well. – warl0ck May 08 '17 at 13:36
  • home page is fine.even its working me, but only setting page only . not showing data ?? – venky May 08 '17 at 13:41
  • i think, if we keep out home page html grid as two column per row means it should work i think so. but not sure about it – venky May 08 '17 at 13:42
  • you have not defined the `presentFilter()` as it shows in the error message – warl0ck May 08 '17 at 14:18
  • I hope now it solves all your problem, hence answers your question – warl0ck May 08 '17 at 14:31
  • which one ? if you are asking about the undefined function `presentFilter()` it should be defined in `about.ts` file as it is called from `about.html` – warl0ck May 08 '17 at 14:44
  • i din get you what you are telling bro .....in about.html what is that it was not defined ?? now tell me aht i have to do alone bro. ia m very confused.. – venky May 08 '17 at 14:57
  • in about.ts file you have to define `presentFilter()` function as you are calling it in about.html – warl0ck May 08 '17 at 14:59
  • bro.no actually thats not a problem. its a normal onclick functionality. i removed that clikc method and tried. even no data showing after first time in my settings.html – venky May 08 '17 at 15:06
  • thats because you have to wait till the data is completely fetched, for that you can use loader to wait till the data is completely fetched, let me update my answer for that as well – warl0ck May 08 '17 at 15:10
  • sure, will check it out. and let you know – venky May 09 '17 at 09:19
  • i tried, but still its showing `please wait` for long time. if the project that u have know if its working, can u please share me that ?? – venky May 09 '17 at 11:30
  • same thing, for home screen also i need to put a please wait message till my data load in screen. if i want to do for my home.ts also how can proceed with that ?? – venky May 09 '17 at 11:33
  • please give solution for that also ?? – venky May 09 '17 at 11:33
  • https://drive.google.com/open?id=0BzQtERw_QqwEa2E3bGgwSHY1emM here is the src folder link – warl0ck May 09 '17 at 11:58
  • does it include the home.ts file too.. i have asked u know about the please wait have to show till the data shoes up ?? – venky May 09 '17 at 12:16
  • yes, it does what you expect it to do, its complete src folder from your application – warl0ck May 09 '17 at 12:16
  • but did you notice that.. under menu if we select `about` , then its not showing the tab bar ?? see here new src with inluded and i added one api call in abouts.html.....when we press from tab its working fine. But when we come from menu and if we press `about` its going to about page and showing data...but no tab bar is showing ....here is that ..https://drive.google.com/drive/folders/0B97QeJKOjA1LN01yLUZXclBpems?usp=sharing – venky May 09 '17 at 12:30
  • you cannot have both sidebar and tab bar see this example http://embed.plnkr.co/cmJYiT/ even here when you select from side menu tab disappear. Now I hope this answers your main question asked atleast ? – warl0ck May 09 '17 at 12:44
  • yes its clear, but if i want to implement that , i need a tab bar to show there ?? what should i need to do ?? – venky May 09 '17 at 13:19
  • hai, can you please rectify this problem... http://stackoverflow.com/questions/43871834/pdf-is-not-opening-in-themeablebrowser – venky May 09 '17 at 13:56
  • I will try to answer :) – warl0ck May 09 '17 at 17:39
  • my http://stackoverflow.com/questions/43883108/ionic-1-themeable-browser-not-opening-in-android-device – venky May 10 '17 at 05:32
  • my demo project https://drive.google.com/file/d/0B97QeJKOjA1LWE94RXo3U3g1Ulk/view?usp=sharing – venky May 10 '17 at 05:32
  • any solution bro for this http://stackoverflow.com/questions/43889322/ionic-2-themeable-browser-not-working-only-white-screen-in-android-device – venky May 10 '17 at 10:25
  • dude give me some time I have my work to as well, will let you know if I will solve your problem – warl0ck May 10 '17 at 10:26
  • sorry bro..its bit urgent.. or else today my job gone. that why asked bro. sorry – venky May 10 '17 at 10:29