什么是'bind'函数?

时间:2018-03-10 14:16:15

标签: javascript angular bind

我对名为bind的函数有一些疑问。

我正在测试Angular中实现的一些公共源代码。 我试图了解绑定功能,我搜索互联网但无法找到正确的答案。你能为此提供一些指导吗?

export class AppComponent {

  currentPage: number = 1;

  news: Array<any> = [];

  scrollCallback;

  constructor(private hackerNewsSerivce: HackerNewsService) {

    this.scrollCallback = this.getStories.bind(this);

   }

  getStories() {
    return this.hackerNewsSerivce.getLatestStories(this.currentPage).do(this.processData);
  }

  private processData = (news) => {
    this.currentPage++;
    this.news = this.news.concat(news.json());
  }

}

1 个答案:

答案 0 :(得分:0)

就角度而言,函数绑定用于在component / directive中声明一个函数,并在scope / this中定义。

我们在需要的地方使用像volatile