【发布时间】:2020-05-02 13:47:50
【问题描述】:
在 Angular 1.7 中使用 UI-Router,我需要更改状态并等待内容加载,然后滚动到页面底部。
// Class method
onCreate() {
this.post().then(response => {
this.$state.go('root.post.edit', { postId: response.data.id });
this.Alert.register('success');
})
.then(() => scrollToBottom()) // This happens too soon
.catch(e => this.handleError(e));
}
//scrollToBottom.js
const scrollToBottom () => {
const scrollContainer = document.querySelector('.app-page-content');
scrollContainer.scrollTop = scrollContainer.scrollHeight;
};
如果我将 scrollToBottom 函数内容包装在 setTimeout(() => {...}, 600) 中,那么它就有时间加载并滚动到加载页面的底部。但是,没有它,页面没有高度,因为它在计算 scrollContainer.scrollHeight 时处于加载状态。
有没有办法等待内容加载到您要导航的路线上?
【问题讨论】:
标签: javascript angularjs dom routes angular-ui-router