gpt4 book ai didi

javascript - 我怎样才能干燥这个 Controller ( Angular 1.5)

转载 作者:行者123 更新时间:2023-11-28 12:19:19 25 4
gpt4 key购买 nike

我有一个带有按钮的组件。单击该按钮时,它会调用两个后端服务之一。调用的服务取决于组件的使用位置。到目前为止,我正在向组件 Controller 传递一个标志,如下所示......

<run-report is-wizard="true" </run-report>

使用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;
});
}
}

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

最佳答案

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

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

关于javascript - 我怎样才能干燥这个 Controller ( Angular 1.5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com