简体   繁体   English

Angular Observable.Timer不更新URL

[英]Angular observable.timer not updating url

I have user observable.timer to fetch data after every one minute.Below is the code 我有用户observable.timer每隔一分钟获取数据。下面是代码

getNewData(){
    var catId = this.serv.getSelectedCategory();
          var sinceId = this.myArray[0].userdataId;
            var dataurl = '';
            if (catId < 0) {
                dataurl = this.baseUrl +  '?bookMarked=' + isBookmarked + '&count=' + this.count + '&isPrevious=' + isPrevious + '&sinceId=' + sinceId;

            }
            else {
                dataurl = this.baseUrl + 'company/' + catId + '/abc' + '?bookMarked=' + isBookmarked + '&count=' + this.count + '&isPrevious=' + isPrevious + '&sinceId=' + sinceId;

            }

            var self = this;

            var headers = new Headers();
            headers.append('Content-Type', 'application/x-www-form-urlencoded');
            headers.append('Authorization', token);

            return Observable.timer(60000,60000)
                .switchMap(() => this.http.get(dataurl , { headers: headers, method: 'GET' }))
                .map((res: Response) => {
                    var obj = res.json();
                    return obj;
                });

        }

In the component 在组件中

this.newDataSubscription = this.testServ.getNewData(false,false).subscribe(data => {
            this.newdataArray = this.serv.newdataArray ;
        })

The issue is the same url is being hit everytime the timer executes.I want to update the sinceId in the url based on the new data that is fetched. 问题是每次执行计时器时都会击中相同的URL。我想根据获取的新数据更新URL中的sinceId。

So everytime the timer executes the sinceId must be the 0th element of the array. 因此,每次计时器执行sinceSinceId必须是数组的第0个元素。

Please Guide 请指导

It seems like this.myArray is the source your IDs and the which ones you want to include in your requests. 看起来像是this.myArray是您的ID的来源以及您想在请求中包括的ID。

getNewData() {    
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization', token);

    return Observable.timer(60000,60000)
        .switchMap(() => {
            this.myArray.forEach(e => {
                // you can avoid too many vars, they are here just with purpose of clarification
                const sinceId = e.userdataId;
                const dataurl = buildUrl(e.userdataId);
                return this.http.get(dataurl , { headers: headers, method: 'GET' }) ;
            });
        })
        .map((res: Response) => {
            var obj = res.json();
            return obj;
        });
}

buildUrl(sindeId) {
    const catId = this.serv.getSelectedCategory();
    let dataurl = '';

    if (catId < 0) {
        dataurl = `${this.baseUrl}?bookMarked=${isBookmarked}&count=${this.count}&isPrevious=${isPrevious}&sinceId=${sinceId}`;
    }
    else {
        // be careful here, you are hardcoding '/abc'
        dataurl = `${this.baseUrl}company/${catId}/abc?bookMarked=${isBookmarked}&count=${this.count}&isPrevious=${isPrevious}&sinceId=${sinceId}`;
    }

    return dataurl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM