我正在将数据发送到ngrx store。之后,我想要滚动到一个特定的div
,该div
正在使用来自store的数据。
@ViewChild('datalist') private myScrollContainer: ElementRef;
this.store.dispatch(new SetClientSearchResultsAction(filteredData));
setTimeout(() => {
this.myScrollContainer.nativeElement.scrollIntoView({ behavior:'smooth', block: 'start'});
}, 300);
以下是HTML的div块。
<div #datalist id="mydata" *ngIf="clientSearchResults$ | async as searchResults"
class = 'result'>
<p> hellooo</p>
</div>
在将数据分配到存储中后,我会在我的 div
中获取滚动条。但我不想使用 setTimeout
,因为这样做是 不必要的等待300毫秒。有没有其他的 替代方法? 我只想在分配数据或ngif条件得到满足时滚动到我的 div
。
下面是我的组件构造函数,在其中我从Store中获取值。
constructor(private store: Store<AppState>,
private formBuilder: FormBuilder, private _clientService: ClientService) {
this.clientSearchResults$ = this.store.select('searchResults');
}
scrollIntoView
是一个实验性功能,不被所有浏览器支持。https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView。请参阅所提供链接的浏览器兼容性。 - amu