Angular 角形头';当数据更改时不更新

Angular 角形头';当数据更改时不更新,angular,typescript,rest,ionic-framework,Angular,Typescript,Rest,Ionic Framework,我有一个subscribe-in-typescript,它从我自己的ASP.NET数据库中提取位置。但是,他们只提取一次数据,但位置应该实时更新。我如何让我的可观察对象自动更新 我曾尝试使用ChangeDetectorRef,但没有解决问题(无论是.MarkForCheck()还是.DetectChanges()) getLocations(){ log(“全部获取!”); this.API.getAllLocations().subscribe(结果=>{ 这个位置=结果; console.l

我有一个subscribe-in-typescript,它从我自己的ASP.NET数据库中提取位置。但是,他们只提取一次数据,但位置应该实时更新。我如何让我的可观察对象自动更新

我曾尝试使用ChangeDetectorRef,但没有解决问题(无论是.MarkForCheck()还是.DetectChanges())

getLocations(){
log(“全部获取!”);
this.API.getAllLocations().subscribe(结果=>{
这个位置=结果;
console.log(这个位置);
这个.addLocationMarkers();
此.ref.markForCheck()为;
});
}
getAllLocations():可观察{
返回此.u http.get(this.url+“location/”,this.headers);
}

我可以清楚地看到console.log(this.locations)在我的浏览器中只调用过一次。当数据库中的基础数据发生更改时,为什么不调用它?

这可以使用Rxjs库完成

在您的ts文件中

import "rxjs/Rx";


numberSubscription: Subscription;

getLocations(){
console.log("Get them all!"); 
const data = Observable.interval(10000).startWith(0); <= this is the time interval in ms at which you want check for change of data in database.
    this.numberSubscription = data.subscribe((number: number) => {
        this.API.getAllLocations().subscribe(result =>{
                    this.locations = result;
                    console.log(this.locations);
                    this.addLocationMarkers();
                    this.ref.markForCheck();
                });
    });
  }
导入“rxjs/Rx”;
数字订阅:订阅;
getLocations(){
log(“全部获取!”);
常数数据=可观测的间隔(10000)。起始值(0){
this.API.getAllLocations().subscribe(结果=>{
这个位置=结果;
console.log(这个位置);
这个.addLocationMarkers();
此.ref.markForCheck()为;
});
});
}

希望这有帮助

这可以通过使用Rxjs库来完成

在您的ts文件中

import "rxjs/Rx";


numberSubscription: Subscription;

getLocations(){
console.log("Get them all!"); 
const data = Observable.interval(10000).startWith(0); <= this is the time interval in ms at which you want check for change of data in database.
    this.numberSubscription = data.subscribe((number: number) => {
        this.API.getAllLocations().subscribe(result =>{
                    this.locations = result;
                    console.log(this.locations);
                    this.addLocationMarkers();
                    this.ref.markForCheck();
                });
    });
  }
导入“rxjs/Rx”;
数字订阅:订阅;
getLocations(){
log(“全部获取!”);
常数数据=可观测的间隔(10000)。起始值(0){
this.API.getAllLocations().subscribe(结果=>{
这个位置=结果;
console.log(这个位置);
这个.addLocationMarkers();
此.ref.markForCheck()为;
});
});
}

希望这有帮助

这不是
http
请求的工作方式。完成后,它将关闭。如果您想要实时更新,您可能需要查看web套接字:每当数据库上发生更新时,它不会影响您的当前数据,除非您调用并订阅相应的http调用。我同意Harun所说的,您可能正在寻找一个Web套接字,您可能想要使用socket.io或其他。因此,我在getAllLocations()中所做的不是订阅http调用?而且,我有一种印象,那就是订阅一个Observable会注意任何更改?难道不是这样吗@KShewenggerIt监视流的更改。但正如@HarunYılmaz所提到的,Http请求完成后,它就关闭了。因此,您将不会收到来自流的任何进一步通知。与其他流(如鼠标移动或用户键入事件)不同,Http请求/响应是“一个完成”。这不是
Http
请求的工作方式。完成后,它将关闭。如果您想要实时更新,您可能需要查看web套接字:每当数据库上发生更新时,它不会影响您的当前数据,除非您调用并订阅相应的http调用。我同意Harun所说的,您可能正在寻找一个Web套接字,您可能想要使用socket.io或其他。因此,我在getAllLocations()中所做的不是订阅http调用?而且,我有一种印象,那就是订阅一个Observable会注意任何更改?难道不是这样吗@KShewenggerIt监视流的更改。但正如@HarunYılmaz所提到的,Http请求完成后,它就关闭了。因此,您将不会收到来自流的任何进一步通知。与其他流(如鼠标移动或用户键入事件)不同,Http请求/响应是“一个完成”的。我认为这里存在不止一个问题。首先,嵌套订阅
rxjs
不鼓励这样做。您可以使用
pipe
map
操作符之一避免它。其次,这不是真正的实时,这是
轮询
,它将保持每10秒发送一次http请求。我认为这不是@MyNameIsGuzse所要求的。在这种情况下,我们可以使用Ngondestry(){this.numberSubscription.unsubscribe();}这只会在呈现组件时起作用,在不需要时不会订阅api。我想这里有不止一个问题困扰着我们。首先,嵌套订阅
rxjs
不鼓励这样做。您可以使用
pipe
map
操作符之一避免它。其次,这不是真正的实时,这是
轮询
,它将保持每10秒发送一次http请求。我认为这不是@MyNameIsGuzse所要求的。在这种情况下,我们可以使用Ngondestory(){this.numberSubscription.unsubscribe();}这样只在呈现组件时有效,在不需要时不会订阅api