I'm working on IONIC and I'm trying to do a "dynamic" side menu.
My problem is to call a method from the side-menu "component".
Well there is no problem to call it if the method was in the "TS file" related to the "HTML file" like this <ion-label (click)="onSearch()"> But the method onSearch() is not in the "TS file" related to the "HTML file". Let me explain...
The side-menu as been made like this:
in app.component.html
<ion-app>
<app-side-menu></app-side-menu>
<ion-router-outlet id="mainContent" main></ion-router-outlet>
</ion-app>
in the side-menu.html component
<ion-menu slide="start" type="overlay" contentId="mainContent" menuId="slidingMenu1" id="slidingMenu1">
<ion-header>
<ion-toolbar color="primary">
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-menu-toggle auto-hide="false">
<ion-item>
<ion-icon name="Search" color="primary" slot="start"></ion-icon>
<ion-label (click)="onSearch()">
Search
</ion-label>
</ion-menu-toggle>
</ion-list>
</ion-content>
</ion-menu>
and I have a service, CoreMenuService, that call the side-menu, like this :
constructor(public menu: MenuController) { }
// Toggle the Menu1 and hide the one you do not need
public toggleMenu1() {
this.menu.enable(true, 'slidingMenu1');
this.menu.toggle('slidingMenu1');
then in the page I need I will call the side menu:
HTML File
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-button (click)="onOpenMenu()"><ion-icon name="menu"></ion-icon></ion-button>
</ion-buttons>
<ion-title>person</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<p class="ion-padding">It's the Person page</p>
</ion-content>
TS File where I want use the method trigger by the side-menu
export class PersonPage implements OnInit {
constructor( public menu: CoreMenuService ) {}
ngOnInit() {}
onOpenMenu() {
this.menu.toggleMenu();
}
onShare() {
console.log('shared');
}
}
So how can I use the method declared in the PersonPage.ts, onShare() from the side-menu component ?
I hope it's clear for you, and that you understand me well. :)
There is the source code if you need it to understand better :
Code
Thanks for your help
Try this
In your side menu component.ts you can do something like:
import { PersonPage } from './../../pages/person/person.page';
export class SideMenuComponent implements OnInit , OnDestroy {
onShare() {
const personPage = new PersonPage(null);
personPage.onShare();
}
}
Related
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
I'm new to coding and I need the "add" Icon to be replaced with "add-circle" icon when users click on it. please help me. Below is the addToCart method in my ts file, what do I have to add in here to replace properly? Please help me
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Courses
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let item of courseList">
<ion-thumbnail slot="start">
<img [src]=item.picture>
</ion-thumbnail>
<ion-label>
<h2>{{item.title}}</h2>
<p> {{item.price | currency}}</p>
<ion-button size="small" id="adding" (click)=addToCart(item)>
<ion-icon slot="icon-only" name="add"></ion-icon>
</ion-button>
</ion-label>
</ion-item>
</ion-list>
</ion-content>
async addToCart(item: Course) {
this.cartService.add(item);
const toast = await this.toastController.create({
message: item.title + ' added to compare',
duration: 2000,
position: 'top',
color: 'secondary'
});
toast.present();
}
}
Declare a variable that has an icon name
public icon: string;
and initialize the icon variable in the constructor
constructor() {
this.icon = 'add';
});
}
and in the addToCart() method assign new add-circle icon
addToCart(item){
this.icon ="add-circle"
}
and finally in the html file
<ion-icon name="{{icon}}"></ion-icon>
Live Demo
Html file
<ion-icon [name]="addCircleIcon" (click)='addCircle()'></ion-icon>
Declare a variable an icon name:
addCircleIcon: string = 'add-icon';
click() function:
addCircle() {
this.addCircleIcon = this.addCircleIcon === 'add-icon' ? 'add-icon-circle' : 'add-icon';
}
[Print screen] [1]: https://i.stack.imgur.com/VWLBX.png
and this is the home.page.html
<ion-header >
<ion-toolbar >
<ion-title>HOME</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<div [innerHTML] id="days"></div>
</ion-content>
Ionic 4 has a bunch of changes and the onClick is no longer valid. You should use <ion-button (click)="myCallback()">
I am following along this Ionic 2 tutorial and encountered problems. The problems are in the TypeScript (See the Picture). Here is the video tutorial I followed.
Here are the src/pages/home/home.html:
<ion-header>
<ion-navbar primary *navbar>
<ion-title>
Tasker
</ion-title>
<ion-buttons end>
<button ion-button icon-only>
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngIf="!task.length">
No Task Available
<p> Click <ion-icon name="add"> to add task</ion-icon></p>
</ion-item>
<ion-item-sliding *ngFor="#t of tasks">
<ion-item>
<ion-toggle></ion-toggle>
<ion-label>
<h2 [ngClass]="t.status">{{t.task}}</h2>
<p [ngClass]="t.priority">{{t.priority}}</p>
</ion-label>
</ion-item>
<ion-item-options>
<button primary><ion-icon name="clipboard"></ion-icon>Edit</button>
<button danger><ion-icon name="trash"></ion-icon>Delete</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
And the src/pages/home/home.ts Where the error occurred!:
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
static get parameters(){
return [[NavController]]
}
constructor(nav) {
this.nav = nav;
this.tasks = [
{task:'test1', priority:'low', status:'pending'},
{task:'test2', priority:'high', status:'done'},
{task:'test3', priority:'normal', status:'pending'}
]
}
}
There are a few typescript issues I see:
You dont need static get parameters function at all.
If you are injecting NavController, you can specify it like so:
constructor(private nav:NavController) {
//this.nav = nav; This is not required if you have set access
//specifier in constructor parameter
// removed rest of code for brevity
}
Lastly, if you need to create a class variable in Typescript, you need to declare in the class.
export class HomePage {
tasks:any[]=[]
// contstructor and other code
}
Note the reference video seems to be using a much older version of Ionic. I suggest you find a recent tutorial video.
Im trying to implement a list with radio buttons using the ion-list and ion-radio directives. The code below is self explanatory, I hope.
I'm only learning ionic now and might be doing something rightfully dumb. please let me know if there's any other details that you require.
Here is My Output. Any clue what I'm doing wrong ?
My html :-
<ion-navbar *navbar>
<button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Select your location</ion-title>
</ion-navbar>
<ion-content padding class="select-location">
<ion-list radio-group>
<ion-list-header *ngIf="!selectLocation">
Where are you located?</ion-list-header>
<ion-list-header *ngIf="selectLocation">
Selected Location:
<p [innerText]="selectLocation.name"></p>
</ion-list-header>
<ion-radio
*ngFor="let location of locations"
(select)="selectLocation = location"
[value]="location.code"
>{{location.name}}
</ion-radio>
</ion-list>
</ion-content>
My ts:-
[import {Component} from '#angular/core';
import {NavController} from 'ionic-angular';
/*
Generated class for the SelectLocationPage page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
#Component({
templateUrl: 'build/pages/select-location/select-location.html',
})
export class SelectLocationPage {
public locations;
constructor(public nav: NavController) {
this.locations = \[
{
code: 1,
name: 'Powai',
pincode: '400076'
},
{
code: 2,
name: 'Chandivali',
pincode: '400072'
},
{
code: 3,
name: 'Dadar',
pincode: '400023'
},
{
code: 4,
name: 'Colaba',
pincode: '400001'
}
\];
}
}][1]
That's some weird looking code; it should look more like this:
<ion-list radio-group [(ngModel)]="selectLocationName">
<ion-list-header *ngIf="!selectLocationName">
Where are you located?</ion-list-header>
<ion-list-header *ngIf="selectLocationName">
Selected Location:
<p> {{ selectLocationName }}</p>
</ion-list-header>
<ion-item *ngFor="let location of locations">
<ion-label>{{ location.name }}</ion-label>
<ion-radio [value]="location.name "</ion-radio>
</ion-item>
</ion-list>