如果我使用$ 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