2016-09-24 3 views
0

웹 소켓을 사용하여 간단한 알림 시스템을 설정하려고합니다.새 데이터에서 Dom이 업데이트되지 않습니다.

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); 
     }); 
    } 
} 

서비스 알림 주제에 가입 구성 요소 :

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); 
    } 
} 

알림 서비스 알림이 포함

나는 내 웹 소켓 서비스가

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) 
    }); 
    } 

} 

을 그래서 내 websocket 서버는 제목으로 고유하게 식별 할 수있는 알림을 보냅니다. 제목이 내 알림 배열에 이미있는 경우 진행 상황을 업데이트하고 그렇지 않으면 밀어 넣습니다.

한 가지를 제외한 모든 것이 정상적으로 작동합니다. 모든 알림 업데이트에서 내보기가 업데이트되지 않지만 아무 곳이나 클릭하면 클릭 이벤트가 다시 렌더링되고 진행 상황이 표시됩니다.

Angular2가 데이터를 전파하는 방법에 대한 몇 가지 기사를 읽었으며 독서회에서 피사체에 가입하고 데이터가 변경되면 다시 렌더링하기 위해 이벤트를 발생시켜야한다는 사실을 알게되었지만 분명히 그렇지 않습니다. 여기의 경우.

무엇이 누락 되었습니까? (현재 사용 Angular2.0.0-rc.5) 나는 "수있었습니다이 질문의 도움으로 내 문제를 해결하기 위해했습니다

답변

관련 문제