Executing COMPONENT method from SERVICE

后端 未结 1 1197
余生分开走
余生分开走 2021-01-28 03:52

I\'m trying to execute a Component method from a Service method. I saw these other 2 threads:

Link1 - How to call component met

相关标签:
1条回答
  • 2021-01-28 04:35

    You can pass a callback to your service and set it as handler for your action sheets.

    public presentActionSheet(
        actionTitle: string, 
        items: any[],
        callbackHandler:any): void {
    
        let actionSheet = this.actionSheetCtrl.create({
            title: actionTitle
        });
        for (let i = 0; i < items.length; i++) {
            actionSheet.addButton({
                text: items[i].name,
                cssClass: items[i].css,
                icon: items[i].selectIcon,
                handler: () => {
                    callbackHandler(i, items[i]);//call the handler passed
                    console.log('Service ActionHandler');
                }
            });
        }
        actionSheet.addButton({ text: 'Cancel', 'role': 'cancel' });
        actionSheet.present();
        }
    

    In your component

    public presentActionSheet(): void {
    //calling the service method passing options for the action sheet 
    this.actionSheetService.presentActionSheet(this.actionTitle,this.types as Type[],this.updateSelectItem.bind(this) );//send your component function. Do not forget to bind the context
            }
    
    0 讨论(0)
提交回复
热议问题