简体   繁体   English

Dom不更新新数据

[英]Dom not updating on new data

I am trying to setup a simple notification system using websockets. 我正在尝试使用websockets建立一个简单的通知系统。

I have my websocket service: 我有我的websocket服务:

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

@Injectable()
export class WebSocketService {
  private socket: Subject<MessageEvent>;

  public connect(url): Subject<MessageEvent> {
    if (!this.socket) {
      this.socket = this.create(url);
    }
    return this.socket;
  }

  private create(url): Subject<MessageEvent> {
    let ws = new WebSocket(url);
    let observable = Observable.create(
      (obs: Observer<MessageEvent>) => {
        ws.onmessage = obs.next.bind(obs);
        ws.onerror = obs.error.bind(obs);
        ws.onclose = obs.complete.bind(obs);
        return ws.close.bind(ws);
      }
    );
    let observer = {
      next: (data: Object) => {
        if (ws.readyState === WebSocket.OPEN) {
          ws.send(JSON.stringify(data));
        }
      },
    };
    return Subject.create(observer, observable);
  }
}

The notification service contains the notifications: 通知服务包含以下通知:

import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Rx'
import {WebSocketService} from "./websocket.service";

@Injectable()
export class NotificationService {
  public messages: Subject<Object>;

  constructor(wsService: WebSocketService) {
    let notificationUrl = `ws://localhost:4969/Notification`;
    this.messages = <Subject<Object>>wsService
      .connect(notificationUrl)
      .map((response: MessageEvent): Object => {
        return JSON.parse(response.data);
      });
  }
}

The component which subscribe to the service notifications subject: 订阅服务通知主题的组件:

import {Component, OnInit, ElementRef} from '@angular/core';
import {Notification} from "./notification";
import {NotificationService} from "../../services/notification.service";

@Component({
  selector: 'notifier',
  templateUrl: 'notifier.component.html',
  styleUrls: ['notifier.component.css'],
  host: {
    '(document:click)': 'onClickedOutside($event)',
  },
})
export class NotifierComponent implements OnInit {
  notifications: Notification[] =[];

  constructor(private elementRef: ElementRef, private notificationService: NotificationService) {
  }


  ngOnInit() {
    this.notificationService.messages.subscribe(notification => {
      let not = new Notification(notification.title, notification.notificationTypes, notification.data);
      console.log(not);

      let index = this.notifications.findIndex(x=>x.title == not.title);
      if (index ==-1) {
        this.notifications.push(not);
      }
      else {
        this.notifications[index].data.progress=not.data.progress;
      }
      console.log(this.notifications)
    });
  }

}

So my websocket server sends notifications which can be uniquely identified by it's title. 因此,我的websocket服务器发送的通知可以通过其标题唯一地标识。 If the title is already present in my notifications array, I updated it's progress, otherwise I push it. 如果标题在通知数组中已经存在,则我会更新进度,否则会推送它。

Everything works fine except one thing. 除了一件事,一切都正常。 My view doesn't update on every notifications updates, but if I click anywhere, the click event will re-render and I will see the progress go up. 我的视图不会在每次通知更新时都更新,但是,如果我单击任意位置,则click事件将重新呈现,并且进度会上升。

I have read a few articles on how Angular2 propagates data and from my reading I've learned that when you subscribe to a subject and it's data changes, it should fire an event to re-render, but that is obviously not the case here. 我已经阅读了几篇有关Angular2如何传播数据的文章,从我的阅读中我了解到,当您订阅某个主题并且数据发生更改时,它将触发一个事件以进行重新渲染,但是显然情况并非如此。

What am I missing? 我想念什么? (currently using Angular2.0.0-rc.5) (当前使用Angular2.0.0-rc.5)

I"ve been able to solved my problem with the help of this question: How to force a component's re-rendering in Angular 2? 我已经能够借助以下问题解决了我的问题: 如何在Angular 2中强制重新渲染组件?

For further reference here is the exact line which I added in the subscribe function of my component: 为了进一步参考,这是我在组件的订阅函数中添加的确切行:

this.applicationRef.tick();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM