2017-01-05 41 views
1

我已經放在一個簡單的例子,我相信應該工作..但它不:( 我需要幫助瞭解我的'在做錯誤的。從可觀察主題推/置值,不更新訂閱可觀察

@Component({ 
    templateUrl: 'sandbox.template.html' 
}) 
export class SandBoxPage implements OnInit { 

    dataSubject: Subject<string> = Subject.create(); 
    data$: Observable<string>; 

    constructor(private platform: Platform) { 
     this.data$ = this.dataSubject 
      .asObservable() 
      .startWith("First value in the observable"); 
    } 

    onClick() { 
     console.log("onClick()"); 
     this.dataSubject.next(Date.now + " value"); 
    } 

    ngOnInit() { 
     console.log("ngOnInit()"); 
     this.data$.subscribe((v) => { 
      console.log("new value", v); 
     }, (err) => { 
      console.log("Error", err); 
     }); 
    } 
} 

我必須掛在onClick()但在我的console.log認購不火,它只有在開始的startsWith值觸發一次按鈕。

回答

1

如果你創建了一個題目create,你需要給它observer和根據文檔的參數。

Rx.Subject.create(觀察員,觀察到的)

來源:https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md#rxsubjectcreateobserver-observable

解決方案: 只是新

dataSubject: Subject<string> = new Subject<string>(); 

來源創建:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

+0

哇。只是......哇。多麼微妙的頭痛。謝謝! – Matt

+0

@Matt我們都在那裏:) – echonax