Javascript 反复检查布尔值
我正在尝试为我的牌构建无限卷轴。 我正在data.service中使用变量Javascript 反复检查布尔值,javascript,angular,typescript,observable,Javascript,Angular,Typescript,Observable,我正在尝试为我的牌构建无限卷轴。 我正在data.service中使用变量reload,检查是否需要加载更多数据,当到达页面末尾时,这些数据被设置为true。 变量设置为真由应用程序组件完成 内容填充是在post.component中完成的 “reload”出现在data.service中,该服务使用http服务从php服务器获取内容。 目前,我正在使用observable并尝试反复访问重新加载状态,但它在Init上只被订阅了一次。 应用程序组件.ts post.component.ts
reload
,检查是否需要加载更多数据,当到达页面末尾时,这些数据被设置为true。变量设置为真由应用程序组件完成
内容填充是在post.component中完成的
“reload”出现在data.service中,该服务使用http服务从php服务器获取内容。
目前,我正在使用observable并尝试反复访问重新加载状态,但它在Init上只被订阅了一次。
应用程序组件.ts
post.component.ts
数据服务.ts
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{BehaviorSubject,Observable};
@注射的({
providedIn:'根'
})
导出类数据服务{
私有重载:行为主体;
构造函数(专用http:HttpClient)
{
this.reload=新行为主体(false);
}
fetchGap=5;//使用此设置每次获取所需的结果数
fetchEnd:number=0;
setValue(newValue):无效{
this.reload.next(newValue);
}
getValue():可观察{
返回此.reload.asObservable();
}
getPostData(){
this.fetchEnd+=this.fetchGap;
返回此.http.get('http://localhost:1234/Server/getPosts.php?fetchEnd=“+this.fetchEnd+”&fetchGap=”+this.fetchGap);
}
}
问题在于治疗后您没有将重新加载的值设置为false。
尝试检查post.component.ts内部。下面是该代码的一部分,用于重新加载的块
post.component.ts重新加载块
this.data.getValue().subscribe((value) => {
if(value){
this.data.getPostData().subscribe(data =>
{
this.posts.push(data);
this.received='success';
},
error =>
{
this.received='error';
});
this.data.setValue(false);
}
});
你现在每次向下滚动都在控制台中翻页?是的。它的打印精细尝试在getvalue中打印值。在post组件中订阅?这也很好…我的实际问题是如何反复检查
重新加载的值。目前订阅部分在Init上只工作一次。
import { Component, OnInit} from '@angular/core';
import {DataService} from './data.service';
import { DomSanitizer } from '@angular/platform-browser';
import {Title} from "@angular/platform-browser";
@Component({
selector: 'app-post-card',
templateUrl: './post-card.component.html',
styleUrls: ['./post-card.component.css']
})
export class PostCardComponent implements OnInit {
constructor(private data: DataService,public sanitizer: DomSanitizer,private titleService:Title)
{
this.titleService.setTitle("Current Feed | Cosmos");
}
received = 'none';
posts: any = [];
ngOnInit()
{
this.data.getPostData().subscribe(data =>
{
this.posts.push(data);
this.received='success';
},
error =>
{
this.received='error';
});
this.data.getValue().subscribe((value) => {
this.data.getPostData().subscribe(data =>
{
this.posts.push(data);
this.received='success';
},
error =>
{
this.received='error';
});
});
}
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {BehaviorSubject, Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
private reload: BehaviorSubject<boolean>;
constructor(private http: HttpClient)
{
this.reload = new BehaviorSubject<boolean>(false);
}
fetchGap=5; // use this to set number of results wanted per fetch
fetchEnd:number= 0;
setValue(newValue): void {
this.reload.next(newValue);
}
getValue(): Observable<boolean> {
return this.reload.asObservable();
}
getPostData(){
this.fetchEnd+=this.fetchGap;
return this.http.get('http://localhost:1234/Server/getPosts.php?fetchEnd='+this.fetchEnd+'&fetchGap='+this.fetchGap);
}
}
this.data.getValue().subscribe((value) => {
if(value){
this.data.getPostData().subscribe(data =>
{
this.posts.push(data);
this.received='success';
},
error =>
{
this.received='error';
});
this.data.setValue(false);
}
});