相关疑难解决方法(0)

Angular 2 - 在模型更改后查看未更新

我有一个简单的组件,它每隔几秒就调用一次REST api并收回一些JSON数据.我可以从我的日志语句和网络流量中看到返回的JSON数据正在发生变化,我的模型正在更新,但是视图没有变化.

我的组件看起来像:

import {Component, OnInit} from 'angular2/core';
import {RecentDetectionService} from '../services/recentdetection.service';
import {RecentDetection} from '../model/recentdetection';
import {Observable} from 'rxjs/Rx';

@Component({
    selector: 'recent-detections',
    templateUrl: '/app/components/recentdetection.template.html',
    providers: [RecentDetectionService]
})



export class RecentDetectionComponent implements OnInit {

    recentDetections: Array<RecentDetection>;

    constructor(private recentDetectionService: RecentDetectionService) {
        this.recentDetections = new Array<RecentDetection>();
    }

    getRecentDetections(): void {
        this.recentDetectionService.getJsonFromApi()
            .subscribe(recent => { this.recentDetections = recent;
             console.log(this.recentDetections[0].macAddress) });
    }

    ngOnInit() {
        this.getRecentDetections();
        let timer = Observable.timer(2000, 5000);
        timer.subscribe(() => this.getRecentDetections());
    }
}
Run Code Online (Sandbox Code Playgroud)

我的观点看起来像:

<div class="panel panel-default">
    <!-- Default panel contents --> …
Run Code Online (Sandbox Code Playgroud)

angular

111
推荐指数
4
解决办法
17万
查看次数

为NgModule和Component提供服务之间的区别

我们刚刚开始使用angular2的第一个项目.我有一个关于提供服务的问题.

据我所知,有两种方法可以providers:[MyService]在您的应用中声明提供商.您可以在@NgModule标记中全局声明它,也可以在标记中本地声明它@Component.

据我所知,这两种方式的唯一区别在于提供范围.适用范围广,一次只有组件范围.出于这一点,我会得出结论,我应该更喜欢在我的特定组件中提供本地服务(取决于服务的用途)以保持范围小.

这是正确的,还是两种宣言方式之间还有其他差异,我不知道?

angular

5
推荐指数
1
解决办法
653
查看次数

标签 统计

angular ×2