【发布时间】:2018-04-13 19:22:36
【问题描述】:
我问了here,我还在苦苦挣扎。所以在谷歌搜索了很多之后,我正在考虑使用 $timeout。我愿意接受任何其他建议。
我的最终目标是仅在屏幕上更新计数后向用户显示警报消息。
我不知道如何使用 $timeout。我收到错误:
'(res: any, $timeout: any) => void' 类型的参数不能分配给'(value: Inspection) => void | 类型的参数PromiseLike'
在发布other question 后,我稍微更改了代码。我已在我的 REST 调用中添加了一个布尔元素以验证计数是否达到阈值,并且我已将实际 REST 调用移至 Service.ts
使用下面的代码,我会在更新屏幕上的计数之前收到警报消息 组件.ts
updateCount(inspection, rankName: string, rankPlusOrMinus: string)
{
inspection.rankName = rankName;
inspection.rankPlusOrMinus = rankPlusOrMinus;
this.inspectionService
.update(inspection).then(function(res) {
alert("Reached Threshold: " + res.thresholdReached);
})
.catch(function(rej) {
console.log(rej);
});
}
service.ts
update(inspection: Inspection): Promise<Inspection> {
const url = '/api/inspection/updatecount/';
let rankName = inspection.rankName;
return this.http
.post(url, JSON.stringify(inspection), {headers: this.headers})
.toPromise()
.then(response => {
//this.alertService.success(`Successfully created associate "${associate.name}"!`, true);
let data = response.json();
if (rankName='A') inspection.rankAcount = data.rankAcount;
if (rankName='B') inspection.rankBcount = data.rankBcount;
if (rankName='C') inspection.rankCcount = data.rankCcount;
return response.json() as Inspection;
})
.catch(error => {
});
}
当我在 component.ts 中传递 $timeout 时出现错误
updateCount(inspection, rankName: string, rankPlusOrMinus: string)
{
inspection.rankName = rankName;
inspection.rankPlusOrMinus = rankPlusOrMinus;
this.inspectionService
.update(inspection).then(function(res, $timeout) {
$timeout(function layout() {
alert(res.thresholdReached);
}, 30);
})
.catch(function(rej) {
console.log(rej);
});
}
【问题讨论】:
-
你为什么要使用
$timeout?那是一个 AngularJS(又名 Angular v1.x)的东西。您正在使用 Angular(又名 Angular v2.x+),所以只需使用setTimeout(...)浏览器方法。 -
我是 Angular 的新手。我正在使用 Angular 4。代码可能对我有很大帮助..谢谢
-
真的很惊讶为什么要减票???我已经把我尝试过的所有代码都放在了里面,并解释了我面临的问题。令人沮丧的是没有评论为什么会有减票......
-
@SKumar 如果您不知道 AngularJS 与 Angular 不是同一个框架,并且您以某种方式设法认为 $timeout 是 Angular 的一部分,那么您的帖子严重缺乏研究。这基本上相当于尝试在 Java 程序中使用您在 Internet 上找到的一段 C++ 代码。
-
如果我是正确的,你是想从“/api/inspection/updatecount/”中获取一些数字吗?当你得到那个数字时,你会尝试获得阈值吗?确认一下,以便我为您提供帮助。
标签: javascript angular typescript ecmascript-6