这就是我所拥有的:
//settings.ts
public messages : Array<Message>;
public static getInstance() {
if (this.instance == null) {
this.instance = new Settings();
}
return this.instance;
}
this.client.onMessageArrived = (message: Message) => {
this.messages.push(message); //new message is pushed in to the array
};
//log.component.ts
public msgs: Array<String>;
Settings.getInstance().messages.forEach(element => {
this.msgs.push(element.getText()); //here I get the messages from settings.ts
});
//log.component.html
<ng-container *ngFor="let msg of msgs">
<md-card>{{msg}}</md-card>
</ng-container>
所以我想要的是:每当一个新的Messege到来时,html也应该显示新的消息 .
我知道我必须使用Observables,我确实在互联网上寻求帮助,但我仍然不知道如何解决这个问题....
感谢您的任何帮助!
1 回答
以下代码可以帮助您,
该服务应修改为