阅读" keydown"输入由空格分隔

时间:2017-05-07 20:17:56

标签: rxjs

我希望通过加入用户输入的键来显示标记(单词),直到她击中空格或进入。这在JS中似乎微不足道,但在rxJs中我一直在尝试很多方法并且卡住了!

请从此开始:

const source = Rx.Observable.fromEvent(someDiv, 'keydown')
//.YOUR_SOL
.subscribe(w=>{console.log(w)})

例如: ' A'' B'' C',空间,' A'' B' => " abc"," ab"

非常感谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

const keyDown$ = Rx.Observable.fromEvent(document, 'keydown').pluck('key');

const isSpaceOrEnter = x => [' ', 'Enter'].includes(x);

const spaceOrEnter$ = keyDown$.filter(isSpaceOrEnter);

keyDown$
  .filter(x => !isSpaceOrEnter(x))
  .buffer(spaceOrEnter$)  
  .map(x => x.join(""))
  .subscribe(x => console.log(x));

您可以在此处进行测试:https://jsfiddle.net/abwtf2xu/1/