如下图,为什么示例1在我们返回div然后单独返回一个新的click observable flatMap to 时会失败click$
?
示例 2 工作正常。JSBin 下面试试看
任何人都可以解释为什么会发生这种情况?据我了解,flatMap 扩展了 Observable。 http://jsbin.com/sowodi/3/edit?js,控制台,输出
// Example 1
() => {
let stream = Rx.Observable.fromArray([1, 2, 3]);
let div$ = stream.map(i => {
let div = document.createElement('div');
div.innerHTML = `NOT WORKING DIV ${i}`;
return div;
})
div$.subscribe(div => {
document.querySelector('body').appendChild(div);
})
let click$ = div$.flatMap(
div => Rx.Observable.fromEvent(div, 'click')
);
click$.subscribe(click => console.log('click'));
}();
// Example 2
() => {
let stream = Rx.Observable.fromArray([4, 5, 6]);
let click$ = stream.flatMap(i => {
let div = document.createElement('div');
div.innerHTML = `DIV ${i}`;
document.querySelector('body').appendChild(div);
return Rx.Observable.fromEvent(div, 'click');
})
click$.subscribe(click => console.log('click'));
}();
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="app">
THE APP ID
</div>
</body>
</html>