0
我有這個組件(和另一個具有相同問題)。當字段值更改視圖不更新時,我甚至試圖強制更改檢測,但它不起作用。Angular2查看變量更改後沒有更新
Angular-cli 1.0.0-beta.10
Angular 2.0.0-rc.4
Component.js
import {Component, OnInit, OnDestroy, ChangeDetectorRef} from '@angular/core';
import {Control} from '@angular/common';
import {WebsocketService} from './websocket.service';
@Component({
moduleId: module.id,
selector: 'chat',
template: `<div *ngFor="let message of messages">
{{message.text}}
</div>
<input [(ngModel)]="message" /><button (click)="sendMessage()">Send</button>`,
providers: [WebsocketService]
})
export class DummyWebsocketComponent implements OnInit, OnDestroy {
messages = [];
connection;
message;
constructor(private chatService:WebsocketService, private changeDetector:ChangeDetectorRef) {
}
sendMessage() {
this.chatService.sendMessage(this.message);
this.message = '';
}
ngOnInit() {
this.connection = this.chatService.getMessages().subscribe(message => {
this.messages.push(message);
console.info(this.messages);
this.changeDetector.markForCheck();
},
error => console.error(error),
() => this.changeDetector.markForCheck()
);
}
ngOnDestroy() {
this.connection.unsubscribe();
}
}
chatService.getMessages
的返回一個Observable
。