angular - Angular表单,成功提交表单后如何自动滚动到顶部?
问题描述
我找不到适合我的角度形式的可行解决方案。基本上,我希望在成功捕获表单后自动滚动回顶部。
我能够捕获提交并重置表单,但是在表单重置后尝试自动滚动回顶部时遇到了一些困难。
这是我onSubmit()
在我的功能account-payable.component.ts
:
onSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.accountPayableForm.invalid) {
return;
}
this.loading = true;
this.accountPayableService
.submitAccountPayable(this.accountPayableForm.value)
.pipe(first())
.subscribe(
data => {
this.alertService.success(
'Success! Account payable created with reference ID: ' +
data.valueOf(),
true
);
this.loading = false;
this.submitted = false;
this.accountPayableForm.reset();
this.goToTop();
},
error => {
this.alertService.error(error);
this.loading = false;
}
);
}
我的goToTop()
功能似乎不起作用:
goToTop() {
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
}
我尝试替换this.goToTop();
为,window.scrollTo(0,0);
但效果不佳,我的表单仍停留在底部,并且我的成功消息显示在表单上方,提交后我必须手动向上滚动以检查。
有什么建议吗?谢谢。
解决方案
如果您只想滚动到顶部,请使用:
window.scrollTo(0,0);
推荐阅读
- python - 从数据框中制作适合特定特征的列表
- javascript - 如何排除 div 内的元素不受 div 函数的影响?
- c# - Winforms 数据库 Id 主键不断计数
- angular - Ag-Grid AutoGroupColumnDef 在 setColumnDef() 函数调用后如何工作?
- scala - 合并包含重复值的数据框的 n 行
- elasticsearch - csv文件中的logstash数据导入错误
- r - 通过R中的变量填充数据框日期
- reactjs - Separate colors for stroke, fill
- javascript - Cloud Firestore + VueJs 自动生成的数字发票号码
- windows - GHC 找不到与 mingw gcc(但不是 mingw-w64)一起使用的外部库(libjack.lib)