如果我使用$ createObservableFunction方法創建一個observable並且我多次訂閱該可觀察值。最後的用戶覆蓋任何其他用戶。
但是,如果我使用rx.Observable.interval()創建一個observable並預訂了多次。這兩個用戶都在這段時間內開火。
爲什麼?更重要的是,如何獲得$ createObservableFunction來觸發兩個訂戶。
app.controller('MainCtrl', function($scope, rx) {
var test = $scope.$createObservableFunction('testClick');
var test2 = rx.Observable.interval(3000);
test.subscribe(function(){
console.log('I never run, why?');
});
test.subscribe(function(){
console.log('Why am I overriding the above subscribe');
});
test2.subscribe(function(){
console.log('This observable runs both subscribed functions')
});
test2.subscribe(function(){
console.log('See this gets called and so does the above');
});
});
舉例說明問題的解決方案。 http://plnkr.co/edit/kXa2ol?p=preview
所以我想我找出了爲什麼它不起作用。 $ createObservableFunction()使用Rx.Observable.create()方法返回訂閱者的SingleCast實現。爲了多播它,你需要做$ createObservableFunction().black().refCount()來保持與源的連接。 – 2014-10-31 01:44:59
爲了記錄,rx.angular的更新版本默認是這樣做的。 – PhiLho 2015-11-24 11:22:03