区间1中的Angular2 http.get不刷新视图

时间:2016-05-24 13:21:03

标签: angular

我有一个简单的角度模型2.我有每秒执行html.get的功能,但放在视图中的变量不刷新自身。有什么问题?

constructor(private _cm: Communication) {}

ngOnInit() {
    setInterval(() => { 
        this.getDeviceData();
        this.numCountChange.next(this.responseDate)
    }, 1000);
}

ngOnDestroy() {
    clearInterval(this.requestInterval);
}

getDeviceData() {
    this._cm.devicesService.getAccessPointStateByIdDeviceAndIdAccessPoint(10018, 10154)
            .subscribe(
                accessPoint => {
                    this.device0 = accessPoint;
                    this.device0.stateChecker();
                    this.responseDate = new Date()
                    console.log(this.responseDate);
                },
                error => console.log(error)
            );
}

HTML:

                    < div class="small-box-footer">
                        {{ responseDate }}
                    < /div>

responseDate始终显示第一个响应值

1 个答案:

答案 0 :(得分:1)

它应该可以在不手动调用更改检测的情况下工作,但我不知道例如devicesService.getAccessPointStateByIdDeviceAndIdAccessPoint实际上在做什么以及代码是什么样的。

至少应该解决这个问题:

constructor(private _cm: Communication, private cdRef:ChangeDetectorRef) {}

ngOnInit() {
    setInterval(() => { 
        this.getDeviceData();
        this.numCountChange.next(this.responseDate)
    }, 1000);
}

ngOnDestroy() {
    clearInterval(this.requestInterval);
}

getDeviceData() {
    this._cm.devicesService.getAccessPointStateByIdDeviceAndIdAccessPoint(10018, 10154)
            .subscribe(
                accessPoint => {
                    this.device0 = accessPoint;
                    this.device0.stateChecker();
                    this.responseDate = new Date()
                    console.log(this.responseDate);
                    this.cdRef.detectChanges()
                },
                error => console.log(error)
            );
}