【发布时间】:2023-04-01 20:00:01
【问题描述】:
在then 回调中的成功工厂响应中调用成功:
这个不行,找不到response:
this.storeFactory.getSafes().then(success(response), failure());
如何正确地将响应传递给success 函数?
var success = (response: any): void => {
scope.safes = response.data.safes;
localStorage.setItem('safeCount', scope.safes.length);
this.$http.get('/app/dashboard/safes/safes.html', { cache: this.$templateCache }).success((tplContent): void => {
element.replaceWith(this.$compile(tplContent)(scope));
});
}
长手版很好用,但是感觉很乱。
this.storeFactory.getSafes().then((response: any): void => {
scope.safes = response.data.safes;
localStorage.setItem('safeCount', scope.safes.length);
this.$http.get('/app/dashboard/safes/safes.html', { cache: this.$templateCache }).success((tplContent): void => {
element.replaceWith(this.$compile(tplContent)(scope));
});
}
【问题讨论】:
-
这并不能回答您的问题或任何问题,但我想为您澄清一下,这不是 TypeScript 语法。这是 ES2015 语法。它是纯 JavaScript,只是更新。 TypeScript 恰好实现了它。
-
好点,我正在将 javascript 转换为 typescript 并启动 ES6 (2015)
-
您的第一次尝试没问题。如果你想表明函数没有返回任何东西,你实际上可以写
var success = (response: any): void => { ... }。问题一定出在其他地方 -
@BrunoGrieder 这一定是我要调用它的地方:
this.storeFactory.getSafes().then(success, failure);如果我添加success()我会收到错误 -
问题已编辑以更能反映障碍
标签: javascript angularjs typescript ecmascript-6 arrow-functions