eya eya - 1 month ago 68
TypeScript Question

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 !

Answer

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.