简体   繁体   English

使用rxjs5运算符fromEvent时获取“ this”的上下文吗?

[英]Get the context of “this” when using the rxjs5 operator fromEvent?

How do I get the this context when using fromEvent observable? 使用fromEvent observable时如何获得this上下文?

window.addEventListener("scroll", function(event) {
    var top = this.scrollY;
   console.log(top)
}, false);

But how do I get it when I do fromEvent? 但是,当我执行fromEvent时如何获得它?

 Observable.fromEvent(window, 'scroll')
          .subscribe((event) => {
            console.log(this.scrollY);
          });

From mdn : 来自mdn

the value of this inside the handler is a reference to the element. 处理程序内部的this的值是对该元素的引用。 It is the same as the value of the currentTarget property of the event argument that is passed to the handler 它与传递给处理程序的事件参数的currentTarget属性的值相同

So you could just do event.currentTarget.scrollY . 所以你可以做event.currentTarget.scrollY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM