Javascript/Angular:包裹在函数中的超时不适用于布尔值
Javascript/Angular: timeout wrapped in function not workink with boolean value
在视图中我有这个条件:
<h3 *ngIf="show">{{users.result}}</h3>
在我的 TypeScript 逻辑中:
show=false; <----as a property
以及以下函数:
timeOut(seconds: number, value:boolean) {
value = true;
setTimeout(
function() {
value = false;
}.bind(this),
seconds
);
}
但是当我调用它时,像这样:
console.log(this.timeOut(3000, this.show));
属性 `this.show´ 未定义,但作为参数传递的秒数有效。我遗漏了一些东西,我不知道是什么...有人可以帮忙吗?
如我所见:
- 首先你控制台记录 函数调用没有结果。
- 当您将布尔值传递到函数参数中时,它被复制,所以当您更改函数内部的值时,它不会影响外部variable/field。
- 这是非常专业的用例,因此您不需要将其提取到不同的功能。
我的建议 - 只需将 setTimeout 调用 箭头函数 放入某些组件的方法中,例如 ngAfterViewInit 或 事件处理程序方法 :
ngAfterViewInit() {
setTimeout(() => this.show = true, 3000)
}
希望对您有所帮助。
在视图中我有这个条件:
<h3 *ngIf="show">{{users.result}}</h3>
在我的 TypeScript 逻辑中:
show=false; <----as a property
以及以下函数:
timeOut(seconds: number, value:boolean) {
value = true;
setTimeout(
function() {
value = false;
}.bind(this),
seconds
);
}
但是当我调用它时,像这样:
console.log(this.timeOut(3000, this.show));
属性 `this.show´ 未定义,但作为参数传递的秒数有效。我遗漏了一些东西,我不知道是什么...有人可以帮忙吗?
如我所见:
- 首先你控制台记录 函数调用没有结果。
- 当您将布尔值传递到函数参数中时,它被复制,所以当您更改函数内部的值时,它不会影响外部variable/field。
- 这是非常专业的用例,因此您不需要将其提取到不同的功能。
我的建议 - 只需将 setTimeout 调用 箭头函数 放入某些组件的方法中,例如 ngAfterViewInit 或 事件处理程序方法 :
ngAfterViewInit() {
setTimeout(() => this.show = true, 3000)
}
希望对您有所帮助。