我想在初始化我的輪播之後在jQuery中執行「each」函數。但是,在ngOnInit()函數中,我有一個與http.get不同步的任務來獲取我需要初始化傳送帶的數據。在異步任務後執行jquery
我的代碼:
ngOnInit() { this.list(); $('.carousel').carousel(); }
list(): void {
this.http.get(`http://localhost:8082/api/list`)
.subscribe(response => {
this.c= response.json().data;
}, error => {
this.error = error;
})
}
ngAfterViewInit() {
// Your jQuery code goes here
$('.carousel .item').each(function() {
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
for (var i = 0; i < 2; i++) {
next = next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
}
的每個功能不執行...
使用信號燈,你從異步調用響應後,修改某些全局變量,或一些DOM元素,並檢查持續在每個函數之前,只有在異步調用修改後才能繼續。 –
初始化您的異步方法回調內的傳送帶。 – ADyson