Showing a different tab with an if statement on Ionic 2 - ionic-framework

Ionic 2 seems very different from 1. I used the starter project and those are my files:
Tabs.html
<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
</ion-tabs>
Tabs.ts
import { Component } from '#angular/core';
import { SigninPage } from '../Auth/Signin/Signin';
#Component({
templateUrl: 'Tabs.html'
})
export class TabsPage {
tab1Root: any = SigninPage;
constructor() {
}
}
app.components.ts
import { Component } from '#angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { TabsPage } from '../pages/Tabs/Tabs';
#Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
rootPage = TabsPage;
constructor(platform: Platform) {
platform.ready().then(() => {
StatusBar.styleDefault();
});
}
}
Is there any way to show a different tab with an if condition? For example, I want to show Tabs.App.html or Tabs.Auth.html depending on an if block.
What is the best way to do it with Ionic 2?

In ionic2 every page is accompanied by it's .ts file. So if you want 2 tabs, app.html and auth.html you would need to create both these pages and their .ts file.
In that way you can write an if-statement saying:
if(1 > 2){
this.rootpage = AuthTab;
}else{
this.rootpage = AppTab;
}
ps don't forget to import your components

Related

How to implement rating in ionic 3?

How do I create 5 star rating in ionic 3, I visited https://github.com/andrucz/ionic2-rating but it is not working. Also read some tutorial but not found anything clean and clear for ionic 3.
Just for clear, I want to get rating value and display rating.
Any help would be appreciated.
Thankyou
Demo: https://stackblitz.com/edit/ionic3-star-rating
Use github.com/melwinVincent/ionic3-star-rating npm package.
Step 1. Add the ionic3-star-rating component in your page (parent component) as follows
<ionic3-star-rating
activeIcon = "ios-star"
defaultIcon = "ios-star-outline"
activeColor = "#488aff"
defaultColor = "#f4f4f4"
readonly="false"
[rating]="3">
</ionic3-star-rating>
Step 2.
You have to import the StarRatingModule in the module.ts of your parent component as follows
import { NgModule } from '#angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ParentPage } from './parent';
import { StarRatingModule } from 'ionic3-star-rating';
#NgModule({
declarations: [
ParentPage,
],
imports: [
StarRatingModule,
IonicPageModule.forChild(ParentPage),
],
})
export class ParentPageModule {}
Step 3.
To get the changed rating in the parent component,
You need to use events from ionic package.
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { Events } from 'ionic-angular';
#Component({
selector: 'page-about',
templateUrl: 'about.html'
})
export class AboutPage {
constructor(public navCtrl: NavController,
public events: Events) {
events.subscribe('star-rating:changed', (starRating) => {console.log(starRating)});
}
}

ionic 3 doesn't showing data json in html

l’m developing app which uses a json file it works perfectly in local server, but isn't display weather data from an API Json .
Here is the code :
RedditData.ts
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
import 'rxjs/add/operator/map';
#Injectable()
export class RedditData {
private url : string = "http://api.wunderground.com/api/d8585d80376/lang:AR/conditions/geolookup/q/ormm.json";
constructor(public http: HttpClient) {
console.log('Hello RedditData Provider');
}
getRemoteData(){
return this.http.get(this.url)
}
}
home.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { RedditData } from '../../providers/reddit-data/reddit-data';
import { HttpClient } from '#angular/common/http';
import 'rxjs/add/operator/map';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
weather: any={};
constructor(public navCtrl: NavController, public redditService: RedditData) {
}
ionViewDidLoad(){
this.redditService.getRemoteData()
.subscribe(weather => {
console.log(weather)
this.weather.current_observation;
});
}
}
the code working fine in console with out any error , but no information in html
<ion-header >
<ion-navbar color="primary">
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding *ngIf="weather">
<p > {{weather.observation_time}}</p>
</ion-content>
You're not assigning the data to the component property:
ionViewDidLoad(){
this.redditService
.getRemoteData()
.subscribe((weather: any) => {
console.log(weather)
this.weather = weather.current_observation; // <-- here!
});
}

If Var on Storage dont show tabs

i want my App to check if a variable "myAccount" is on native storage to show or not Tabs, i used ionViewCanEnter to check if the variable myAccount is present on local Storage, if its not isConnected will be set to false, if its isConnected will be set to true
here's my code:
tabs.ts:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HomePage } from '../home/home';
import { NativeStorage } from '#ionic-native/native-storage';
import { MyaccountPage } from '../myaccount/myaccount';
/**
* Generated class for the TabsPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-tabs',
templateUrl: 'tabs.html',
})
export class TabsPage {
public isConnected: Boolean= true;
homeRoot = HomePage;
myAccountRoot = MyaccountPage;
rootPage = HomePage;
constructor(public navCtrl: NavController, public navParams: NavParams, private nativeStorage: NativeStorage) {
}
ionViewCanEnter(){
this.nativeStorage.getItem('myAccount')
.then(
//data => this.isConnected=false,
// error => this.isConnected=true
data => this.isConnected=true,
error => this.isConnected=false
);
}
logout(){
this.nativeStorage.remove('myAccount');
this.isConnected=false;
}
}
if isConnected is true it will show the tabs with HomePage as selectedIndex with the tabs visible, if its not it will show the HomePage with no Tabs
tabs.html:
<ion-tabs selectedIndex="0" *ngIf="isConnected" class="tabs-icon-top tabs-color-active-positive">
<ion-tab [root]="homeRoot" tabIcon="home"> </ion-tab>
<ion-tab [root]="myAccountRoot" tabIcon="person"></ion-tab>
<ion-tab [root]="myAccountRoot" tabIcon="chatbubbles"></ion-tab>
<ion-tab (ionSelect)="logout()" tabIcon="power"></ion-tab>
</ion-tabs>
<ion-nav [root]="rootPage" *ngIf="!isConnected">{{isConnected}}</ion-nav>
The probleme is that its not totally working, when myAccount is added to storage, the tabs doesnt show up but if i exit the application and relaunch it, they appear
used ionViewWillEnter and it worked

Cant hide Ionic Tab first

I'm trying to create Ionic-3 Tab option, it's working fine, but my issue is I don't want to display 1st tab in tab menu,but I want to display 1st tab menu in page details in 1st time page opening ,I'm try to hide this 1st tab , but its not working for me, anyone knows how to do that?I have attached some images on my issue to help you understand it.
Tabs.html
<ion-tabs>
<ion-tab [root]="tab0Root"></ion-tab>
<ion-tab [root]="tab1Root" tabTitle="Check-In" tabIcon="people"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Observations" tabIcon="information-circle"></ion-tab>
<ion-tab [root]="tab3Root" tabTitle="Activities" tabIcon="book"></ion-tab>
<ion-tab [root]="tab4Root" tabTitle="Health" tabIcon="medkit"></ion-tab>
</ion-tabs>
Tabs.ts
import { Component } from '#angular/core';
import { AboutPage } from '../about/about';
import { ContactPage } from '../contact/contact';
import { HomePage } from '../home/home';
import {HealthPage} from '../health/health';
import {MainPage} from '../main/main';
#Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
tab0Root = MainPage;
tab1Root = HomePage;
tab2Root = AboutPage;
tab3Root = ContactPage;
tab4Root = HealthPage;
constructor() {
}
}
Every Tab has it own show property. So just change it to false if you want to hide it.
In tabs.ts:
import { Component, ViewChild } from '#angular/core';
import { Tabs } from 'ionic-angular';
#ViewChild(Tabs) tabs: Tabs;
ionViewDidEnter(){
this.tabs.getByIndex(0).show = false;
}
code side:
tab0Root = MainPage;
public isVisibleFirstTab : boolean = false;
mark-up:
<ion-tab *ngIf="isVisibleFirstTab" [root]="tab0Root"></ion-tab>
or
[rootParams] could be used for *ngIf condition

Multiple component in shared.module.ts Ionic

i am new to ionic and recently i tried to create a shared component so that i can sort of remove duplicate coding of same content on different page. It consist of a notification bar and a panel that have a login/signup button. I am able to display the notification bar and the login panel on pages that i want but i am unable to call the function of the login/signup button.
Whenever i click the login/signup button, it will said presentLoginModal() is not a function. Below are my code.
Hope someone can advice me on this. Thanks.
shared.module.ts
import {NgModule} from '#angular/core';
import {IonicPageModule} from 'ionic-angular';
import {NotificationComponent} from '../notification/notification';
import {NonLoginComponent} from '../nonlogin/nonlogin';
#NgModule({
imports: [IonicPageModule.forChild(NotificationComponent),IonicPageModule.forChild(NonLoginComponent)],
declarations: [NotificationComponent, NonLoginComponent],
exports: [NotificationComponent, NonLoginComponent]
}) export class SharedModule {}
nonlogin.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import {Login} from '../login/login';
import {Register} from '../register/register'
#IonicPage()
#Component({
selector: 'app-nonlogin',
templateUrl: './nonlogin.html'
})
export class NonLoginComponent {
constructor(public navCtrl: NavController) {}
presentLoginModal() {
let loginModal = this.modalCtrl.create(Login, {showBackdrop: true, enableBackdropDismiss: true},{cssClass: 'logon'});
loginModal.present();
}
presentRegisterModal(){
let registerModal = this.modalCtrl.create(Register, {showBackdrop: true, enableBackdropDismiss: true},{cssClass: 'register'});
registerModal.present();
}
}
notification.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'app-notification',
templateUrl: './notification.html'
})
export class NotificationComponent {
constructor(public navCtrl: NavController) {}
}
nonlogin.html
<div class="toppanel"><div class="leftside"><img class="companylogo"
src="../../assets/imgs/logo.jpg"></div><div class="rightside">
<button tappable class="login" ion-button
(click)="presentLoginModal()">LOG IN</button><button tappable
class="signup" ion-button (click)="presentRegisterModal()">SIGN
UP</button></div></div>
home.html
<ion-content>
<app-notification></app-notification>
<app-nonlogin></app-nonlogin>
</ion-content>