我想在初始化我的轮播之后在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