异步任务后执行Jquery

时间:2017-03-02 15:34:47

标签: jquery angular typescript

我想在初始化我的轮播后在jQuery中执行“each”功能。但是,在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));
        }
    });
}

每个功能都不执行......

2 个答案:

答案 0 :(得分:1)

这是因为你的async函数的响应还没有。您可以在收到回复后立即调用.each函数。

list(): void {
this.http.get(`http://localhost:8082/api/list`)
    .subscribe(response => {
        this.c= response.json().data;
        // Your jQuery code goes here
        $('.carousel').carousel();
        $('.carousel .item').each() .... 
    }, error => {
        this.error = error;
    })
 } 

答案 1 :(得分:1)

使用信号量;

有一个全局systemctl restart kibana变量;

switch

并保持每个方法执行发生这么小的延迟循环,直到异步调用结束它。您可以选择〜

,而不是全局变量,您可以使用一些DOM元素来执行此操作
相关问题