简体   繁体   中英

Dom not updating on new data

I am trying to setup a simple notification system using websockets.

I have my websocket service:

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. 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.

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.

What am I missing? (currently using 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?

For further reference here is the exact line which I added in the subscribe function of my component:

this.applicationRef.tick();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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