javascript - 为什么箭头函数不传递参数?

标签 javascript ecmascript-6 rxjs arrow-functions

我正在使用 Angular w/rxjs 来观察界面上的用户事件。但是,我在将参数传递给箭头函数中的方法时遇到了这个非常简单的问题。问题是这样的:

这不起作用:搜索词没有传递到 this.searchFacilities

ngOnInit() {
 this.searchTerm$.subscribe(searchterm => this.searchFacilities);/**Not working here**/
}

searchFacilities(term: string){
  console.log(term);
  this.facilityservice.searchFacilities(term)
    .subscribe(results => this.facilityList = results);
}

但这有效:

this.searchTerm$.subscribe(searchterm => { this.searchFacilities(searchterm); })

显然,我还有其他非常轻松的解决方案,但我真的想了解为什么我的第一种方法不起作用。谢谢!

最佳答案

因为参数没有直接传递给你的函数。

示例来自the doc :

Rx.Observable.range(0, 3).subscribe(function (x) { console.log(x) });

带有箭头函数的相同示例:

Rx.Observable.range(0, 3).subscribe(x => console.log(x));

关于javascript - 为什么箭头函数不传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42232447/

相关文章:

javascript - rxjs 可观察管道内的建模和 if/else 模式

javascript - 使用 PHP 和 Javascript 创建翻转

javascript - 如何在 webkit 中使用 SVG* 对象?

javascript - 为什么 for() 函数比 ES6 map() 和 some() 更快地查找重复项?

javascript - 清理 json 包含和使用

html - Angular5 从可观察到组件获取数据

javascript - 将元素定位在图像元素上,但图像随窗口改变大小

JavaScript - 循环内的 If 语句出现问题

javascript - onChange = {this.nameHandler} 可以调用 nameHandler = (event) =>{}

Angular 2 : Updating and sharing data between components