eya
eya

Reputation: 55

ionic 2 - calling pages functions from app.component.ts

I have an ionic 2 appliction containing a menu and every time I select one item from the menu the main page should be updated, for that I wrote a function in my main.ts that returns the wanted result as a list , and in app.component.ts I have to call that function to be executed ..

What should I do please ??

Thanks !

Upvotes: 1

Views: 4020

Answers (1)

Ben
Ben

Reputation: 563

Do I unsderstand your question correctly, how can you fire your function in main.ts from app.components.ts?

You could use Ionic2 Events for this purpose:

In app.components.ts:

import { Events } from 'ionic-angular';
constructor(public events: Events) {
}

clickMenu(item) {
events.publish('menu:clicked', item);
}

Add clickMenu(item) to your menu-item: (click)="clickMenu('whateverYouWantToPass')"

and in main.ts:

import { Events } from 'ionic-angular';

constructor(public events: Events) {
events.subscribe('menu:clicked', (item) => {
  // Do something with the clicked item data, e.g.:
  console.log(item[0]);
});
}

I hope this brings you closer to your solution.

Upvotes: 8

Related Questions