簡體   English   中英

數組正在更新,但視圖不在Angular 2中

[英]Array being updated but view is not in Angular 2

我有一個組件,用於存儲要在列表中顯示的服務器陣列。 組件從服務訂閱可觀察項。

這是組件代碼:

import {Client} from '../../connectionService/client.service';


@Component({
    templateUrl: 'build/pages/page1/page1.html',
    providers: [Client]
})
export class Page1 {
    servers : any;

    constructor(private client: Client) {
      this.servers = [];
      this.client._myServers.subscribe((newServer: any) => {
        console.log("new server!", newServer);
        this.servers.push(newServer);
      });
}

風景:

  <ul>
    <li *ngFor='#item of (servers)'>
     Name : {{item.name}}
  </li>
</ul>

服務:

import { Injectable } from '@angular/core';
import {Observable}     from 'rxjs/Observable';
import {Subject}  from 'rxjs/Subject';


    @Injectable()
    export class Client {
      private serversSubject: Subject<any>;
      _myServers : Observable<ServerHandler>;

    constructor() {
        this.serversSubject = new Subject<any>();
        this._myServers = this.serversSubject.asObservable();
    }

...

當我發現服務器(在與上述相同的服務中:

      this.serversSubject.next({"name": result.name, "adress": result.address});

“新服務器!” 被打印,並且服務器在組件中進行了更新, 但視圖未顯示新項 當我進出頁面時,將顯示新服務器。

預先感謝,馬庫斯

這是一個普遍的問題。 您的藍牙庫可能在Angulars區域之外運行,並且當在Angulars區域之外運行的代碼更新模型時,不會調用Angulars更改檢測。

有多種策略可以手動調用更改檢測。 手動觸發Angular2更改檢測

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM