【发布时间】:2017-03-02 15:34:46
【问题描述】:
初始化轮播后,我想在 jQuery 中执行“每个”函数。但是,在 ngOnInit() 函数上,我有一个与 http.get 的异步任务来获取初始化轮播所需的数据。
我的代码:
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元素,并在
each函数之前不断检查,只有在异步调用修改它之后才继续。 -
从您的异步方法初始化回调中的轮播。
标签: jquery angular typescript