第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Angular 2-模型更改后視圖未更新

Angular 2-模型更改后視圖未更新

開滿天機(jī) 2019-10-15 14:55:41
我有一個(gè)簡單的組件,該組件每隔幾秒鐘調(diào)用一次REST API,并接收一些JSON數(shù)據(jù)。從我的日志語句和網(wǎng)絡(luò)流量中,我可以看到返回的JSON數(shù)據(jù)正在更改,并且我的模型正在更新,但是視圖沒有更改。我的組件看起來像: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());    }}我的看法如下:<div class="panel panel-default">    <!-- Default panel contents -->    <div class="panel-heading"><h3>Recently detected</h3></div>    <div class="panel-body">        <p>Recently detected devices</p>    </div>    <!-- Table -->    <table class="table" style="table-layout: fixed;  word-wrap: break-word;">        <thead>            <tr>                <th>Id</th>                <th>Vendor</th>                <th>Time</th>                <th>Mac</th>            </tr>        </thead>從結(jié)果中可以看到console.log(this.recentDetections[0].macAddress),recentlyDetections對象正在更新,但是視圖中的表永遠(yuǎn)不會改變,除非我重新加載頁面。我正在努力查看我在這里做錯(cuò)了什么。有人可以幫忙嗎?
查看完整描述

3 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

就我而言,我有一個(gè)非常相似的問題。我正在由父組件調(diào)用的函數(shù)中更新視圖,而在父組件中,我忘記了使用@ViewChild(NameOfMyChieldComponent)。我僅僅因?yàn)檫@個(gè)愚蠢的錯(cuò)誤而損失了至少3個(gè)小時(shí)。即:我不需要使用任何這些方法:


ChangeDetectorRef.detectChanges()

ChangeDetectorRef.markForCheck()

ApplicationRef.tick()


查看完整回答
反對 回復(fù) 2019-10-15
  • 3 回答
  • 0 關(guān)注
  • 951 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號