【问题标题】:Executing COMPONENT method from SERVICE从 SERVICE 执行 COMPONENT 方法
【发布时间】:2018-01-08 12:03:30
【问题描述】:

我正在尝试从 Service 方法 执行组件 method。我看到了其他 2 个线程:

Link1 - How to call component method from service?

Link2 - Angular2 call a Component from a Service

但它们显示了从 构造函数 执行的方法。

我在 Ionic 应用程序上使用 Ion-Select 组件(大量选择),为此我使用了一个功能调用操作表,它为从选择中选择的每个选项都有一个回调处理程序。

Action 在服务上,因此 Handler 调用。但是每个选择都有自己的方法,需要从 ActionSheet 中调用。这就是我现在的代码:

选择示例

import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ActionSheetService } from '../../services/action-sheet.service';

import { Type } from '../../models/general/type.model';

@Component({
    selector: 'type-select-component',
    templateUrl: 'type-select.component.html',
    providers: [ ActionSheetService ]
})
export class TypeSelectComponent {

    @Input() public types: Object[];
    @Output() public typeId: EventEmitter<number> = new EventEmitter<number>();

    public updatedItem: Type = new Type();
    private actionTitle: string = "What's Type"

    constructor( private actionSheetService: ActionSheetService ) { }

    //The Service should execute this method
    public updateSelectItem(id:number, type: Type): void{
        this.updatedItem = type;
        this.typeId.emit(id);
    }

    public presentActionSheet(): void {
//calling the service method passing options for the action sheet 
this.actionSheetService.presentActionSheet(this.actionTitle,this.types as Type[]);
        }
    }

行动单服务

import { Injectable } from '@angular/core';
import { ActionSheetController } from 'ionic-angular'

@Injectable()
export class ActionSheetService {

constructor(private actionSheetCtrl: ActionSheetController) { }

public presentActionSheet(
    actionTitle: string, 
    items: 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: () => {
//Here I should execute the method updateSelectItem from the component
                updateSelectItem(i, items[i]);
                console.log('Service ActionHandler');
            }
        });
    }
    actionSheet.addButton({ text: 'Cancel', 'role': 'cancel' });
    actionSheet.present();
    }
}

我为什么要这样做??? 好吧,我可以在每个选择中放置一个操作表......但是它会破坏 DRY

有帮助吗?

【问题讨论】:

    标签: angular ionic-framework ionic2


    【解决方案1】:

    您可以将回调传递给您的服务并将其设置为您的操作表的处理程序。

    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();
        }
    

    在你的组件中

    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
            }
    

    【讨论】:

    • man :D 成功了!但我没有“绑定上下文”不理解这部分......该应用程序与您的修改一起使用。但是我可以解释一下这个 xD XD
    • 好吧.. this.updateSelectItem.bind(this) Bind 函数将上下文设置为当前的this 即组件。否则当它被调用时,this 会指向不同的上下文,this.updatedItem 会出错
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2017-11-25
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多