【发布时间】:2014-10-30 22:54:37
【问题描述】:
如果我使用 $createObservableFunction 方法创建了一个可观察对象,并且我多次订阅该可观察对象。最后一个订阅者覆盖任何其他订阅者。
但是,如果我使用 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');
});
});
说明问题的示例 plunker。 http://plnkr.co/edit/kXa2ol?p=preview
【问题讨论】:
-
所以我想我知道为什么它不起作用了。 $createObservableFunction() 使用 Rx.Observable.create() 方法返回订阅者的 singleCast 实现。为了多播它,您需要执行 $createObservableFunction().publish().refCount() 以保持与源的连接。
-
作为记录,较新版本的 rx.angular 默认会这样做。
标签: angularjs rxjs reactive-extensions-js