How to use 3 ionic-slides in one page and take instance of each ion-slides? [duplicate] - ionic-framework

I am using ionic 3 while placing 3 swiper sliders on same page i am unable to control their behavior separately i am bit new for angular js as well ts and ionic
here is code
<ion-slides style="height: 30%;" pager>
<ion-slide style="background-color: green" *ngFor="let post of posts">
<h1>{{post.title}}</h1>
<img [src]="post.image" />
</ion-slide>
</ion-slides>
<ion-slides style="height: 30%;" pager>
<ion-slide style="background-color: green" *ngFor="let post of posts">
<h1>{{post.title}}</h1>
<img [src]="post.image" />
</ion-slide>
</ion-slides>
ts is
#ViewChild(Slides) slides: Slides;
goToSlide() {
this.slides.slideTo(2, 500);
}
ngAfterViewInit() {
//this.slides.freeMode = true;
this.slides .slidesPerView=3,
this.slides.spaceBetween=5;
//this.slides.loop=true;
}

Instead of ViewChild, you can use ViewChildren to get all the instances of the slider in that page (Angular docs). Please take a look at this working plunker. The end result would be something like this:
Like you can see in the plunker, we get all the instances of the slider, and then we just get the target instance by using its index:
import { Component, ViewChildren, QueryList } from '#angular/core';
import { NavController, Content, Slides } from 'ionic-angular';
#Component({...})
export class HomePage {
#ViewChildren(Slides) slides: QueryList<Slides>;
constructor() {}
public move(index: number): void {
this.slides.toArray()[index].slideNext(500);
}
}
An then in the view:
<ion-header>
<ion-navbar>
<ion-title>Ionic Demo</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-slides style="height: 75px" pager>
<ion-slide><h1>Slide 1</h1></ion-slide>
<ion-slide><h1>Slide 2</h1></ion-slide>
<ion-slide><h1>Slide 3</h1></ion-slide>
</ion-slides>
<ion-slides style="height: 75px" pager>
<ion-slide><h1>Slide 1</h1></ion-slide>
<ion-slide><h1>Slide 2</h1></ion-slide>
<ion-slide><h1>Slide 3</h1></ion-slide>
</ion-slides>
<ion-slides style="height: 75px" pager>
<ion-slide><h1>Slide 1</h1></ion-slide>
<ion-slide><h1>Slide 2</h1></ion-slide>
<ion-slide><h1>Slide 3</h1></ion-slide>
</ion-slides>
<ion-row>
<ion-col>
<button (click)="move(0)" ion-button text-only>Move First</button>
</ion-col>
<ion-col>
<button (click)="move(1)" ion-button text-only>Move Second</button>
</ion-col>
<ion-col>
<button (click)="move(2)" ion-button text-only>Move Third</button>
</ion-col>
</ion-row>
</ion-content>

Related

Ionic how to disable slide on swipe screen?

I have a slide. I want to do slide on button which is done. But i still can slide by gesture. I need to remove my slides from gesture. only slide will do from buttons. Any one can help how can i disable it from gesture ?
Here is my html code
<ion-content>
<div class="slide-box">
<ion-slides pager="true" >
<!-- Slide 1 -->
<ion-slide>
<div>
<img src="../../assets/img/welcome.jpg"/>
<h2>
AI Generated Photos
</h2>
<ion-label color="gray" >
All the photos are generated with artificial intelligence
</ion-label>
</div>
</ion-slide>
<!-- Slide 2 -->
<ion-slide>
<div>
<img src="../../assets/img/welcome.jpg"/>
<h2>
Download Your Photos
</h2>
<ion-label color="gray" >
Select the photo you like to download the photos by buying them
</ion-label>
</div>
</ion-slide>
<!-- Slide 3 -->
<ion-slide>
<div>
<img src="../../assets/img/welcome.jpg" />
<h2>
Follow Us Instagram
</h2>
<ion-label color="gray" >
You can follow us on instagram to download the photos for free
</ion-label>
</div>
</ion-slide>
</ion-slides>
</div>
</ion-content>
<ion-footer style="margin-bottom: 7%;" no-border>
<div style="display: flex; justify-content: center;" (click)="next(slides)" *ngIf="next1">
<ion-button expand="block" class="btn">NEXT</ion-button>
</div>
<div style="display: flex; justify-content: center;" (click)="nextd(slides)" *ngIf="next2">
<ion-button expand="block" class="btn">NEXT</ion-button>
</div>
<div style="display: flex; justify-content: center;" (click)="login()" *ngIf="start" >
<ion-button expand="block" class="btn">GET STARTED</ion-button>
</div>
</ion-footer>
I just want to do that the user can not slide from screen. they can just do it by buttons
Try this way.It will helps You.
HTML:
<ion-slides pager="true" #slider>
Your code here.....
</ion-slides>
TS:
import { Component ,ViewChild} from '#angular/core';
export class HomePage {
#ViewChild('slider') slider;
constructor(public navCtrl: NavController) {
}
ionViewDidLoad(){
this.slider.onlyExternal = true;
}
demo link:https://stackblitz.com/edit/ionic-p48vuq

How to disable Sidemenu on certain pages Ionic 2

i am building a project with ionic 3, and the first 3 pages use the blank ionic template, because it has to do with login, registration and then verification, and afterwards you enter the applications home page, which then i utilized the sidemenu ionic template
i have succeeded in building the application and everything works, but after adding the sidemenu templates it affects the blank templates, if you slide left on the screen the side menu shows..
this is my code..
on the app.html
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div style="height:130px;"><img src="assets/imgs/titlebg.jpg"/> </div>
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}} <ion-badge *ngIf="p.badge" end>234</ion-badge>
</button>
</ion-list>
</ion-content>
<ion-footer>
<p align="center" style="color:#333333"> WihofaCITY.com</p>
</ion-footer>
</ion-menu>
<ion-nav id="nav" #content [root]="rootPage"></ion-nav>
on the verify account view template i have this
<ion-header>
<ion-navbar color="">
<ion-title>Current Subscription</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<!---- content goes here ---->
<ion-grid>
<ion-row>
</ion-row>
</ion-grid>
</ion-content>
i removed the menutoggle button but if you slide on those pages, this is what i did
<ion-navbar>
<!------------i removed this section ----------------->
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<!------------i removed this section ----------------->
<ion-title>Current Subscription</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<!---- content goes here ---->
<ion-grid>
<ion-row>
</ion-row>
</ion-grid>
</ion-content>
but yet to no avail, please is there something i was supposed to do, or is not doing? thanks in advance.
You can enable/disable the sidemenu from your controller like below
<ion-menu [content]="content" id="mymenu">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div style="height:130px;"><img src="assets/imgs/titlebg.jpg"/> </div>
<ion-list>
<button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}} <ion-badge *ngIf="p.badge" end>234</ion-badge>
</button>
</ion-list>
</ion-content>
<ion-footer>
<p align="center" style="color:#333333"> WihofaCITY.com</p>
</ion-footer>
</ion-menu>
And then in your controller, import MenuController
import { MenuController } from 'ionic-angular';
public menuController:MenuController
menuController.enable(true,"mymenu"); //For Enabling
menuController.enable(false,"mymenu"); //For Disabling

How to show Image when No internet connection in Ionic 2

I am Beginner in Ionic 2 . I am working on Network Connection/Detection Using Network native plugin.
I want show wifi symbol image when there is no Internet connection
Example .
And i want to hide this Dashboard when there is no internet connection and required to show wifi image symbol like above image
This is my code for dashboard.html
<ion-grid responsive-sm center >
<ion-row style="background-color: #fff;">
</ion-row>
<ion-row center>
<ion-col (click)="gotomaps()"> <ion-fab >
<button ion-fab > <img src="assets/icon/location.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family:inherit; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Mapping</label>
</ion-grid>
</ion-fab>
</ion-col>
<ion-col width-50 center (click)="gotosendmanager()"> <ion-fab >
<button ion-fab > <img src="assets/icon/folder.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center;margin:8px;color:#A62C23;font-weight: bold"> Send manager</label>
</ion-grid>
</ion-fab>
</ion-col>
</ion-row>
<div class="or-label" text-center hidden>
<img alt="Logo" src="assets/imgs/wifi.png" >
</div>
<ion-row >
<ion-col width-50 (click)="gototabs()"> <ion-fab >
<button ion-fab > <img src="assets/icon/question.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Help</label>
</ion-grid>
</ion-fab>
</ion-col>
<ion-col width-50 (click)="exit()"> <ion-fab >
<button ion-fab > <img src="assets/icon/logout.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Exit</label>
</ion-grid>
</ion-fab>
</ion-col>
</ion-row>
<ion-row style="background-color: #fff;">
</ion-row>
</ion-grid>
I have get perfect image hide and show w.r.to Internet connection with this code
Import Network plugin
import { Network } from '#ionic-native/network';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
Put this under before constructor
hide:boolean = true;
Put this code under Constructor
var offline = Observable.fromEvent(document, "offline");
var online = Observable.fromEvent(document, "online");
offline.subscribe(() => {
this.hide =false;
});
online.subscribe(()=>{
this.hide =true;
});
Put this under html file
<div class="or-label" text-center *ngIf="!hide" >
<img alt="Logo" src="assets/imgs/wifi.png" >
</div>
//Result : When your device Internet is not available then wifi image is visible and wise versa..
I will create for youNetworkConnectionProvider.ts provider for listening network event.
import {Injectable} from '#angular/core';
import {Platform, ToastController, Events} from "ionic-angular";
import {Network} from "#ionic-native/network";
export enum ConnectionStatusEnum {
Online,
Offline
}
#Injectable()
export class NetworkConnectionProvider {
public isOnline: boolean = true;
private previousStatus;
constructor(private network: Network,
private platform:Platform,
private toastCtrl: ToastController,
private eventCtrl: Events) {
this.platform.ready().then(() => {
this.previousStatus = ConnectionStatusEnum.Online;
this.initializeNetworkEvents();
});
}
public initializeNetworkEvents(): void {
this.network.onDisconnect().subscribe(() => {
if (this.previousStatus === ConnectionStatusEnum.Online) {
this.eventCtrl.publish('network:offline');
}
this.previousStatus = ConnectionStatusEnum.Offline;
this.isOnline = false;
});
this.network.onConnect().subscribe(() => {
if (this.previousStatus === ConnectionStatusEnum.Offline) {
this.eventCtrl.publish('network:online');
}
this.previousStatus = ConnectionStatusEnum.Online;
this.isOnline = true;
});
}
}
And then inject in NetworkConnectionProvider in app.module.ts
Provider Uses
In dashboard.ts
First of all inject private networkCheck:NetworkConnectionProvider and private eventCtrl: Events in constructor. Then listen it.
flag:boolean=false;
this.eventCtrl.subscribe('network:online', () => {
// online action
this.flag =true;
});
this.eventCtrl.subscribe('network:offline', () => {
// offline action
this.flag =false;
});
In dashboard.html need to modify
<ion-grid responsive-sm center >
<ion-row style="background-color: #fff;">
</ion-row>
<ion-row center *ngIf="flag">
<ion-col (click)="gotomaps()"> <ion-fab >
<button ion-fab > <img src="assets/icon/location.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family:inherit; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Mapping</label>
</ion-grid>
</ion-fab>
</ion-col>
<ion-col width-50 center (click)="gotosendmanager()"> <ion-fab >
<button ion-fab > <img src="assets/icon/folder.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center;margin:8px;color:#A62C23;font-weight: bold"> Send manager</label>
</ion-grid>
</ion-fab>
</ion-col>
</ion-row>
<div class="or-label" text-center *ngIf="!flag">
<img alt="Logo" src="assets/imgs/wifi.png" >
</div>
<ion-row *ngIf="flag">
<ion-col width-50 (click)="gototabs()"> <ion-fab >
<button ion-fab > <img src="assets/icon/question.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Help</label>
</ion-grid>
</ion-fab>
</ion-col>
<ion-col width-50 (click)="exit()"> <ion-fab >
<button ion-fab > <img src="assets/icon/logout.png"/> </button>
<ion-grid text-center>
<label style="font-size: 15px; font-family: arial; text-align:center ;margin:8px;color:#A62C23;font-weight: bold"> Exit</label>
</ion-grid>
</ion-fab>
</ion-col>
</ion-row>
<ion-row style="background-color: #fff;">
</ion-row>
</ion-grid>
Following #Utpaul answer. You can also use hidden attribute which doesn't delete your element from the Dom like ngIf does.
<ion-grid responsive-sm center [hidden]="flag" >
Maybe this concept can help you..
You can use #ionic-native/network
To check current network connection inside the device
isConnected(): boolean {
let conntype = this.network.type;
return conntype && conntype !== 'unknown' && conntype !== 'none';
}
There is a function like onConnect and onDisconnect inside the ionic-native/network modules, but it only checks whenever you connect/disconnect the network from your phone when you were in-app. and maybe its useful to use the function if someone suddenly has no /disable the network connection
Constructor :
constructor(private network: Network, private toast: ToastController) { }
Simply put in the constructor
if(!this.isConnected()) {
this.openToast(this.isConnected());
}
Simply put inside the constructor of the page, This is how we can automatically check the network connection after the page is loaded
openToast() function
openToast(isConnected: boolean) {
let networkType = this.network.type;
this.toast.create({
message: `Network type: ${networkType} and ${isConnected}`,
duration: 3000
}).present();
}
and take note that in ionic 3.. The supported version of #ionic-native/network < 5.0.0
I tested on Ionic 3 can use version 4.6.0
This is how we declare import { Network } from '#ionic-native/network/index';
More info :
https://www.npmjs.com/package/#ionic-native/network
Result : if the user open up the page with no internet connection,
it's toast up a message
I hope someone found this helpful information

How to make ion-slide responsive?

My ionic app has a slider with 2 slides, and they show up based on left and right swipe action. I want to make my page responsive. In case of large screen i want to show both slides side-by-side and swipe action to be disabled. How can i achieve this?
I think you want to do this so far
<ion-slides autoplay="5000" loop="true" speed="3000" class="full">
<ion-slide>
<img src="assets/img/gym.jpg" />
</ion-slide>
</ion-slides>
This code auto-change your images with the slide animation and it is more responsive also
Responsive grid view is working fine for me here
<ion-content padding class="page-manhomeslider">
<ion-slides autoplay="5000" loop="false" speed="1000" (ionSlideDidChange)="slideChanged()">
<ion-slide>
<ion-grid>
<ion-row>
<ion-col text-left>
<ion-label style="color: black">1/12</ion-label>
</ion-col>
</ion-row>
<img src="assets/gif/men/1-jumping-jacks.gif" class="slide_img"/>
<ion-row>
<ion-col class="pageheader">
<h2>Jumping Jacks</h2>
</ion-col>
</ion-row>
<ion-footer>
<ion-row>
<ion-col>
</ion-col>
<ion-col>
<div id="countdown">
<div class="countdown-number"></div>
<svg>
<circle r="28" cx="40" cy="40"></circle>
</svg>
</div>
</ion-col>
<ion-col>
<ion-buttons class="btnend" end>
<button (click)="goToSlide2()" ion-button round>
<ion-icon name="arrow-forward"></ion-icon>
</button>
</ion-buttons>
</ion-col>
</ion-row>
</ion-footer>
</ion-grid>
</ion-slide>
In React, I used states to update the slidesPerView property whenever the page is resized.
export default function ResponsiveSlides () {
const [slideOpts, setSlideOpts] = useState ({
initialSlide: 0,
speed: 400,
slidesPerView: Math.floor (window.innerWidth / 298),
spaceBetween: 1
});
function changeSlideOpts (key, value) {
setSlideOpts (
{
... slideOpts,
[key]: value
}
)
}
window.onresize = function (event) {
changeSlideOpts ("slidesPerView", Math.floor (window.innerWidth / 298));
};
return (
<IonSlides pager = {false} options = {slideOpts}>
<IonSlide>
<IonCard />
</IonSlide>
<IonSlide>
<IonCard />
</IonSlide>
<IonSlide>
<IonCard />
</IonSlide>
<IonSlide>
<IonCard />
</IonSlide>
<IonSlide>
<IonCard />
</IonSlide>
<IonSlide>
<IonCard />
</IonSlide>
</IonSlides>
)
}

Back Arrow Button In Ionic

I'm new to Ionic and I'm trying to add back arrow to my project using Ionic framework. I'm using the simple code as follows:
<ion-header-bar class="bar-stable">
<ion-nav-back-button class="button-clear">
<i class="ion-arrow-left-c"></i> Back
</ion-nav-back-button>
<h1 class="title">Title of my project</h1>
</ion-header-bar>
But I can't see the Ionic back button in my header anywhere.
Here some code for header in ionic.
<ion-view>
<ion-nav-bar>
<ion-nav-buttons side="left">
<button class="button button-clear ion-arrow-left-c " ng-click="backButton()"></button>
</ion-nav-buttons>
<ion-nav-buttons side="right">
<button class="button button-clear btn-white ion-android-settings header-icon-size " ng-click="openDrawer()"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-content>
<!-- here content -->
</ion-content>
</ion-view>
<ion-header style="background-color: #D3D3D3;box-shadow: none;">
<ion-navbar>
<ion-title>
<ion-row no-padding>
<ion-col no-padding>
<ion-buttons left>
<button ion-button icon-only (click)="back()">
<ion-icon name="arrow-back"></ion-icon>
</button>
</ion-buttons>
</ion-col>
<ion-col>
<div style="text-align: center;font-size: 20px; margin-top: 5px;">
Training Reports
</div>
</ion-col>
<ion-col></ion-col>
</ion-row>
</ion-title>
</ion-navbar>
</ion-header>
ion-nav-back-button is only displayed when your navigation stack has more than one page pushed onto it.
The following code shows this by displaying a page and then pushing the same page onto the navigation stack after 2 seconds. In the first page, the back arrow isn't displayed, but it is displayed on the second page and pressing it returns to the first page. The results can be tested in this JSFiddle that I created (which contains the same code).
HTML:
<script type="module" src="https://cdn.jsdelivr.net/npm/#ionic/core/dist/ionic/ionic.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/#ionic/core/dist/ionic/ionic.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#ionic/core/css/ionic.bundle.css"/>
<ion-app>
<ion-nav root='nav-page'></ion-nav>
</ion-app>
Javascript:
customElements.define(
'nav-page', class NavPage extends HTMLElement
{
connectedCallback() {
this.innerHTML = `
<ion-header translucent>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button defaultHref="/">
</ion-back-button>
</ion-buttons>
<ion-title>
Page ${this.data ? this.data.page : 0}
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content fullscreen>
Page ${this.data ? this.data.page : 0}
</ion-content>
`;
}
});
const nav = document.querySelector('ion-nav');
let data = { page: 1 };
setTimeout(function() {
nav.push('nav-page', { data });
}, 2000);