navCtrl navigation to next page ionic - html

I have some issue with my code. Can't navigate from home page to for example about page. The application is in ionic framework. Totally new in this technology. In console no warnings and errors.
home.ts
import { Component } from '#angular/core';
import { NavController, IonicPage } from 'ionic-angular';
import { Auth } from '#ionic/cloud-angular';
import { User } from '#ionic/cloud-angular';
import { LoginPage } from '../login/login';
import { About } from '../about/about';
import { ChatPage } from '../chat/chat';
#IonicPage()
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public auth: Auth, public user: User) {
console.log(user);
}
logout() {
this.auth.logout();
this.navCtrl.setRoot(LoginPage);
}
about(){
this.navCtrl.setRoot(About);
}
chat(){
this.navCtrl.setRoot(ChatPage);
}
}
home.html
<ion-header>
<ion-navbar>
<ion-title>
Home
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="logout()">
<ion-icon name="exit"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<h2>Welcome back, {{ user.details.name }}</h2>
<p>
<button ion-button color="secondary" block>Something</button>
</p>
<p>
<button ion-button color="secondary" block [navPush]="ChatPage">Chat</button>
</p>
<p>
<button ion-button color="secondary" block [navPush]="About">About</button>
</p>
</ion-content>
about.ts
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'page-about',
templateUrl: 'about.html',
})
export class About {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad About');
}
}
about.html
<ion-header>
<ion-navbar>
<ion-title>about</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="about">
This is my super awesome about page.
</ion-content>

One possible and easy solution is call method on click of button. As you are defining about() method in home.ts now call this method on click of button. just update code in home.html
<ion-header>
<ion-navbar>
<ion-title>
Home
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="logout()">
<ion-icon name="exit"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content padding>
<h2>Welcome back, {{ user.details.name }}</h2>
<p>
<button ion-button color="secondary" block>Something</button>
</p>
<p>
<button ion-button color="secondary" block (click)="chat()">Chat</button>
</p>
<p>
<button ion-button color="secondary" block (click)="about()">About</button>
</p>
</ion-content>

Related

Redirect to another page using angular 6

I am using a template I bought recently to build my ionic + angular app. My problem is that I am new to angular and I am trying to redirect the user when the click a button. I've been researching and I got the point where it detects the click but doesn't redirect. I tried importing "router" and other troubleshooting steps but it's not working for me. Here is my html, and ts file.
html
<!--Theme Google Card - Full Image Cards-->
<ion-content padding-top>
<ion-grid no-padding>
<ion-row *ngIf="data != null">
<ion-col col-12 col-md-6 col-lg-6 *ngFor="let item of data.items;let i = index">
<ion-card text-left box-shadow margin-bottom>
<!--Card Image-->
<div card-image>
<img [src]="item.image" />
<div watch-now text-center (click)="onEvent('onItemClick', item, $event)">
<button ion-button button-icon icon-start>
<ion-icon icon-small [name]="item.icon"></ion-icon>
{{item.iconText}}
</button >
</div>
</div>
<!--Card descriptiom-->
<ion-card-content>
<ion-card-title text-center>
<!--Card Subtitle-->
<h1 card-title>{{item.title}}</h1>
<!-- Icon Rating Star -->
<ion-icon *ngFor="let star of item.ratingStar.iconsStars; let i = index" (click)="onStarClass(item.ratingStar.iconsStars, i, $event)">
<i icon-small *ngIf="star.isActive" class="icon {{star.iconActive}}"></i>
<i icon-small *ngIf="!star.isActive" class="icon {{star.iconInactive}}"></i>
</ion-icon>
<!-- Reviews Star -->
<span span-medium>{{item.reviews}}</span>
<!--Card Body Text-->
<p margin-top card-body-text>{{item.description}}</p>
</ion-card-title>
</ion-card-content>
</ion-card>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
TS
import { Component, Input, ViewChild } from '#angular/core';
import { IonicPage, Content } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'google-card-layout-1',
templateUrl: 'google-card.html'
})
export class GoogleCardLayout1 {
#Input() data: any;
#Input() events: any;
#ViewChild(Content)
content: Content;
slider = {};
constructor() { }
slideHasChanged(slider, index): void {
this.slider[index] = slider;
if (2 == slider._activeIndex) {
if (this.data.items) {
this.data.items.splice(index, 1);
} else {
this.data.splice(index, 1);
}
}
}
onStarClass(items: any, index: number, e: any) {
if (e) {
e.stopPropagation();
}
for (var i = 0; i < items.length; i++) {
items[i].isActive = i <= index;
}
this.onEvent("onRates", index, e);
};
onClickEvent(index): void {
if (this.slider[index]) {
this.slider[index].slidePrev(300);
}
}
onEvent(event: string, item: any, e: any) {
if (e) {
e.stopPropagation();
}
if (this.events[event]) {
this.events[event](item);
}
}
}
Firstly you have to declare your route in your routing file in my case i have one newly created component so i update my route as follow
{
path: 'admin',
canActivate: [AuthGuard],
loadChildren: './pages/admin/admin.module#AdminModule'
},
After that there are two method to navigate,
1) <button [routerLink]="['/admin']" ion-button button-icon icon-start>
2) <button (click)="navigate()" ion-button button-icon icon-start>
and in your ts file add code
navigate() {
this.router.navgateURL(['/admin']);
}
Try:
<button [routerLink]="['/path']" ion-button button-icon icon-start>

How to display Navbar In Webview (InAppBrowser) in IONIC?

I'm develop mobile with IONIC Cordova, and I'm very beginner in this.
I want to question how to display navbar in Page Webview, my page webview use InAppBrowser.
this is my page.html
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>WV</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
</ion-content>
this my page.ts
#IonicPage()
#Component({
selector: 'page-wv',
templateUrl: 'wv.html',
})
export class WvPage {
my_url: any;
constructor(public navCtrl: NavController, public navParams: NavParams, private IAB: InAppBrowser) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad WvPage');
}
ngOnInit(){
const browser = this.IAB.create('http://myUrl.com/', '_self', {location: 'no'});
}
}

How to set image url in html from typescript?

I'm building ionic app for wallpaper/gallery.I want app to show wallpaper in full screen when user clicks on a particular image.
Thanks.
HTML code:
<ion-header>
<ion-navbar color="dark">
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Walldo</ion-title>
</ion-navbar>
</ion-header>
<ion-slides zoom="true">
<ion-slide>
<div class="swiper-zoom-container">
<img id="fullwall" src="some link">
</div>
<ion-label>Elephant</ion-label>
<button ion-button round (click)="download('wall.jpg')">Download</button>
</ion-slide>
</ion-slides>
TYPESCRIPT code:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
#IonicPage()
#Component({
selector: 'page-popular',
templateUrl: 'popular.html',
})
export class PopularPage {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad PopularPage');
}
}
It's a simple property binding:
<img [src]="theWallImageUrl" />
and in the code of the component:
this.theWallImageUrl = ...

IONIC 2 navbar left and right button not working

Because of the title issue in ionic 2 for android. i have set some css and make centre of title.and i have put the left and right button in the nav bar. but when i apply the onclick functionality for that two button.its not working. even no console message too.
here is my code :
html :
<ion-header>
<!-- use ion-toolbar for a normal toolbar and ion-navbar for navigation -->
<ion-toolbar>
<ion-buttons class="loginnavbtn" (click)="goback()" left>
CANCEL
<!-- left aligned content here -->
</ion-buttons>
<ion-title>
LOGIN
</ion-title>
<ion-buttons class="loginnavbtn" (click)="loginbtntap()" right>
SAVE
<!-- left aligned content here -->
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content>
</ion-content>
My css :
ion-header {
.button-md {
box-shadow: none;
}
.toolbar-title {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
font-size: 15px;
font-weight: 500;
}
}
my js :
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
constructor(public navCtrl:NavController) {
}
public goback() {
this.navCtrl.pop();
}
public loginbtntap() {
this.navCtrl.pop();
}
}
My onclick is not working.what i ma doing wrong ?
Thanks !!
Here is the solution that works.
<ion-header>
<ion-navbar>
<ion-buttons *ngIf="user != null" left>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
</ion-buttons>
<ion-buttons *ngIf="user == null" left>
<button ion-button (click)="loginBtnClicked()">
<ion-icon name="log-in"></ion-icon>
</button>
</ion-buttons>
<ion-title>Home</ion-title>
<ion-buttons right>
<button ion-button (click)="searchClicked()">
<ion-icon name="search"></ion-icon>
</button>
</ion-buttons>
I faced the same issue in ionic3.
Please use button tag with ion-buttons and this solution works for me.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<ion-header>
<!-- use ion-toolbar for a normal toolbar and ion-navbar for navigation -->
<ion-toolbar>
<button ion-buttons class="loginnavbtn" (click)="goback()" left>
CANCEL
<!-- left aligned content here -->
</button>
<ion-title>
LOGIN
</ion-title>
<button ion-buttons class="loginnavbtn" (click)="loginbtntap()" right>
SAVE
<!-- left aligned content here -->
</button>
</ion-toolbar>
</ion-header>
<ion-content>
</ion-content>
Try to use ion-button
<button ion-button class="loginnavbtn" (click)="goback()" left>Name of button</button>

Bluetooth Serial in Ionic 2, Can't Connect

I'm using Bluetooth serial in my Ionic2 app and I want to connect to the device.
Got this now in the .ts file but doesn't works. I can enable bluetooth but cant connect.
import { Component } from '#angular/core';
import { BluetoothSerial } from 'ionic-native';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public stat_on:any;
public stat_con:any;
public macAddress:"98:D3:31:90:77:36";
constructor(public navCtrl: NavController) {
}
turnON(){
BluetoothSerial.enable().then((status)=> {
this.stat_on=status;
});
}
Connect(){
BluetoothSerial.isEnabled().then((data)=> {
BluetoothSerial.connect("98:D3:31:90:77:36").then((stat_con)=>{
this.stat_con=stat_con;
});
});
}
}
Here is my HTML file
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-fab right bottom>
<button ion-fab color="light"><ion-icon name="arrow-dropleft"></ion-icon></button>
</ion-fab>
<ion-card-content>
<button ion-button (click)="turnON()">Turn ON Bluetooth</button>
<button ion-button (click)="Connect()">Connect</button>
<p>BT: {{ stat_on }}</p>
<p>Status: {{ stat_connect }}</p>
</ion-card-content>
</ion-content>
Thanks before :)