从SERVICE

时间:2018-01-08 12:03:30

标签: angular ionic-framework ionic2

我正在尝试从服务方法执行Component 方法。我看到了其他2个帖子:

Link1 - How to call component method from service?

Link2 - Angular2 call a Component from a Service

但他们展示了从构造函数执行的方法。

我正在使用离子应用程序,Ion-Select组件(很多选择)而且我正在使用一个功能调用操作表,它为从select中选择的每个选项都有一个回调处理程序。

操作在服务上,因此处理程序调用。但每个select都有一个自己的方法需要从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

任何帮助人员?

1 个答案:

答案 0 :(得分:2)

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

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
        }

在您的组件中

Declare @data varchar(max) = '34.22,768.55,34.22,123.34,12,999.0,999.0'

SELECT STUFF(
(
    SELECT DISTINCT ',' + UniqNum FROM
    (
        SELECT CAST('<d>'+replace(@data, ',','</d><d>')+'</d>' AS XML) AS numberXml
    ) as t1
    CROSS APPLY 
    (
     SELECT my_Data.D.value('.','varchar(50)') as UniqNum
     FROM t1.numberXml.nodes('d') as my_Data(D)
    ) t2
    FOR XML PATH('')
), 1, 1, '')