Ionic 2 tabs and sidebar - How do I maintain tabs with setRoot? - tabs

I have an Ionic 2 app that uses both a sidebar and tabs.
I have managed to create a link in the sidebar that sends the user back to the root page, but this kills the tabs. I would like the tabs to be persistent.
Here is what I have:
app.html
`<ion-menu [content]="content">
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
<ion-content>
<ion-list>
<ion-item (click)="openPage(page)" *ngFor="let page of pages">
<span>{{page.title}}</span>
</ion-item>
</ion-list>
</ion-content>
<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>
Then in my app.ts I have this function:
openPage(page) {
this.menu.close();
this.nav.setRoot(page.component);
}
So this does actually work, but I lose my tabs. Is there a better way to do this?

this.nav.setRoot(TabsPage);
TabsPage is the class name of your tabs page's component.

You could reference the ionic-conference-app. It works for me.
openPage(page){
this.menu.close();
let params = {};
if (page.index) {
params = { tabIndex: page.index };
}
if (this.nav.getActiveChildNavs().length && page.index != undefined) {
this.nav.getActiveChildNavs()[0].select(page.index);
} else {
this.nav.setRoot(page.name, params).catch((err: any) => {
console.log(`Didn't set nav root: ${err}`);
});
}
}

Related

How to change the button layout when the Lottie Animation ends in angular?

One of the requirements of our application is to use Lottie to add animation for the onboarding part of the app. The scenario will be, if the Lottie animation has ended, the Skip to Homepage button should change to the Home button. I am having a hard time on how I will implement it with my app. I've been stuck here for almost a week but still, I can't apply to my app. I am quite confused if I applied it correctly. Hope you can help me. I will be adding my codes below for reference. Thank you
.ts
// LOTTIE
animationCreated(animationItem: AnimationItem): void {
this.animationItem = animationItem;
this.animationItem.autoplay = true;
this.animationItem.loop = false;
if (this.animationItem.loop === false) {
this.onLoopComplete();
this.showHomeButton = true;
}
}
onLoopComplete(): void {
this.ngZone.runOutsideAngular(() => {
this.animationItem.addEventListener('loopComplete', () => {});
console.log(
this.animationItem.addEventListener('loopComplete', () => {})
);
});
}
.html
<div class="demo">
<ng-lottie
*ngIf="shown"
containerClass="centerLottie"
width="520px"
height="420px"
[options]="options"
(animationCreated)="animationCreated($event)"
></ng-lottie>
</div>
<ion-row>
<ion-col size="4"></ion-col>
<ion-col size="4">
<div class="ion-text-center ion-no-padding" *ngIf="this.pageInddex_d==1">
<ion-button *ngIf="!showHomeButton" fill="clear" routerLink="/home" size="large" shape="round" color="primary">
Skip to homepage
</ion-button>
<ion-button *ngIf="showHomeButton" expand="full" routerLink="/home" size="large" shape="round" color="primary">
Home
</ion-button>
</div>

passing data to ionic 5 custom element tag

my ionic 5 app has a side menu and tabs. Each page can have different contents for the side menu and the tabs. I don't want to be duplicating the ion-menu in each page so I created a header component like so (I also do one for the tabs):
HEADER COMPONENT:
<ion-menu contentId="content">
<ion-header>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>{{pageObj.title}}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-menu-toggle auto-hide="false" *ngFor="let page of pageObj.pages">
<ion-item [routerLink]="page.url" routerDirection="root" [class.active-item]="selectedPath.startWith(page.url)">
<ion-icon>{{page.icon}}</ion-icon>
<ion-label>
{{page.title}}
</ion-label>
</ion-item>
</ion-menu-toggle>
</ion-list>
</ion-content>
</ion-menu>
Now I use this header component in app.component.html like so:
<ion-app>
<ion-split-pane>
<app-header></app-header>
<ion-router-outlet id="content"></ion-router-outlet>
</ion-split-pane>
</ion-app>
And app.component.ts:
...........
.........
export class AppComponent {
pageObj: any = '';
selectedPath = '';
pages = [
{
title: 'Become a Member',
url: '/pages/membership'
},
{
title: 'Make a Posts',
url: '/pages/posts'
}
]
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private router: Router
) {
this.initializeApp();
this.pageObj.title = 'Menu';
this.pageObj.pages = this.pages;
this.router.events.subscribe((event:RouterEvent) => {
if (event && event.url) {
this.selectedPath = event.url;
}
})
}
.........
........
So the problem is requires variables (pageObg) that exist in app.component.ts. I am not sure how to pass those variables? I believe I can do something like this:
<app-header [pageObj]="pageObj"><app-header>
but I am not sure how this works! Is there a better way to achieve what I am trying to do?
You are on the right path, but that is not enough. You need to create the corresponding #Input pageObj property in the child component's class. That should be sufficient in your case.
This is the comprehensive guide on Component Interaction, please refer to the first section on "input binding" at this link: https://angular.io/guide/component-interaction#pass-data-from-parent-to-child-with-input-binding

Updating a list in ionic modal updates original list ionic 5

I have a clarification, Basically i am passing a list from parent page to Ionic modal. In modal, i will iterating over a list. i am basically modifying a property of an object on click of ion-item.
Below is the html code in the modal.
<ion-card *ngFor="let item of items">
<ion-row>
<ion-col size="3">
<ion-img width="80" height="80" [src]="item.imagePath"></ion-img>
</ion-col>
<ion-col size="7" >
<ion-label class="item-name">item.name</ion-label>
<ion-label class="item-price">item.cost</ion-label>
<ion-label class="item-date">item.date</ion-label>
</ion-col>
<ion-col size="2">
<ion-icon class="select-icon" name="add-circle" (click)="updateObj(item)"></ion-icon>
</ion-col>
</ion-row>
</ion-card>
In modal.component.ts
ngOnInit() {
this.items = this.navParams.get("items");
}
dismissModal(){
this.modalCtrl.dismiss();
}
updateObj(object){
if(object){
object.status = true
}
}
i am using navParams to get the list from parent page to the modal. When i click on ion-item i will be calling updateObj method where i am updating the status of object to true. Everything works fine.
But when i update the object in the modal, the original list sent to the modal also gets updated.
Could anyone explain why updating the list in the modal updates the original list. Is it bcoz of using navParams as it referers to the list?
In Parent.component.ts
async openSearchModal(){
const modal = await this.modalCtrl.create({
component: CouponSearchPage,
componentProps: { items: this.itemListData,
}
});
This is how i am passing the list to the modal from parent screen.
When you pass this.itemListData to modal, you are passing the reference of the object.
If you don't want to modify the actual object, you can pass a new object.
Try this:
const modal = await this.modalCtrl.create({
component: CouponSearchPage,
componentProps: { items: { ...this.itemListData},
}
});

ionic3: How to connect values of radio button to connect to a page

Ionic 3...App contains news page and settings page.
The Settings Page has to change the following settings of the app using radio buttons : • The news source the app reads the news from. changes made on this page should be stored when the Save button is pressed. When the Settings Page is open again these changes should be reflected.
i am unable to link radio buttons to particular news source
If you are not working with some kind of backend you could use Ionic LocalStorage. You need to install the Storage plugin for this to work. To do so follow this link.
In settings.ts you should have something like this.
import { Storage } from '#ionic/storage';
firstOption: String;
export class Settings {
constructor(private storage: Storage) { }
ionViewWillEnter() {
this.setUpSettings();
}
optionSelected(option,value) {
this.storage.set(option,value);
}
setUpSettings() {
this.storage.get('firstOption').then((res) => {
this.firstOption = res;
});
}
}
Then in settings.html
<ion-content>
<ion-list radio-group [(ngModel)]="firstOption">
<ion-item>
<ion-label>BBC</ion-label>
<ion-radio (ionSelect)="optionSelected('firstOption','bbc')" value="bbc"></ion-radio>
</ion-item>
<ion-item>
<ion-label>CNN</ion-label>
<ion-radio (ionSelect)="optionSelected('firstOption','cnn')" value="cnn"></ion-radio>
</ion-item>
</ion-list>
</ion-content>

Google map displayed in modal in Ionic 2

I am developing app in Ionic 2, and I want to display map in a modal. Problem is I can't get it to work. Same code works on normal page, but not in modal. I tried using web version of google maps API and even native cordova plugin, but result is the same, blank map in modal, working map on page. Any advice how to solve this?
You should set the underneath page to transparent.
Here is my working code for calling map modal
html
<ion-content [style.opacity]="isModal ? 0 : 1" padding>
<button ion-button item-right >
<ion-icon name="add" (click)="getLocation()"></ion-icon>
</button>
</ion-content>
typescript
getLocation() {
this.isModal = true;
let mapModal = this.modalCtrl.create(MapModalPage);
mapModal.onDidDismiss((data) => {
this.isModal = false;
if(data) {
this.location = data.location;
this.locImage = data.image;
}
});
mapModal.present();
}