怎么能干掉这个控制器(角度1.5)

Jos*_*ose 0 javascript angularjs

我有一个带按钮的组件.单击该按钮时,它会调用两个后端服务之一.调用的服务取决于组件的使用位置.到目前为止,我正在向组件控制器传递一个标志,就像这样......

<run-report is-wizard="true" </run-report>
Run Code Online (Sandbox Code Playgroud)

isWizard: '<'component.js文件中使用,然后我在按钮的单击事件中有以下代码run-report...

run() {
    if (this.running) {
        return;
    }

    this.running = true;

    //prepare the details for the report
    const reportDetails = this.prepareReportData({
        name: this.reportName,
        settings: this.mapSettings(this.selectedSettings),
    });

    if (this.isWizard) {

        return this.BackendService
            .postWizardReport(reportDetails)
            .then(response => {
                //do stuff
            })
            .finally(() => {
                this.running = false;
            });

    } else {

        return this.BackendService
            .postMainReport(reportDetails)
            .then(response => {
                //do stuff
            })
            .finally(() => {
                this.running = false;
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

我不这样做,因为我正在重复代码.有谁能建议更好的方法?谢谢

Dun*_*can 6

如果唯一的区别是方法的名称,那么使用数组语法而不是点:

var action = this.isWizard? 'postWizardReport' : 'postMainReport';
return this.BackendService
            [action](reportDetails)
            .then(response => {
                //do stuff
            })
            .finally(() => {
                this.running = false;
            });
Run Code Online (Sandbox Code Playgroud)