简体   繁体   English

Typescript组件中的“ this”与Angular2中的ES5

[英]'this' in Component of Typescript vs ES5 in Angular2

So I've been trying to work through converting my Angular2 app from Typescript to ES5 and have been trying to make both of them run similarly. 因此,我一直在尝试将Angular2应用程序从Typescript转换为ES5,并一直试图使两者运行类似。 The problem that I'm having is getting the this.people to update in the ES5 version. 我遇到的问题是让this.people在ES5版本中进行更新。

When I console.log this in the constructor versus in my searchFor method, I get the scoping of the class versus the window's scope. 当我CONSOLE.LOG this构造与我searchFor方法,我得到的类与窗口的范围的范围界定。 When I console.log this in my Typescript version, it stays within the SearchComponent and is the same in both. 当我CONSOLE.LOG this在我的打字稿版本,它保持的SearchComponent内,并在两个相同的。

Typescript Version: 打字稿版本:

import {Component} from 'angular2/core';
import {PersonService} from './person-service';

@Component({
  selector: 'search',
  properties: ['searchTerm'],
  templateUrl: `search.html`,
})

export class SearchComponent {
  searchTerm:string;
  searchPrefix:string;
  people:Person[];

  constructor(private _personService:PersonService) {
    this.searchTerm   = '';
    this.searchPrefix = "";
    this.people       = [];
    this.predicate    = 'last';
  }

  searchFor(term) {
    if (term.length < 2)
      this.people = [];
    else {
      this._personService.getUsers(term)
        .then((people)=> {
          this.people = people;
        });
    }
  }
}

ES5 Version ES5版本

(function(app) {
  app.SearchComponent = ng.core
    .Component({
      selector: 'search',
      properties: ['searchTerm'],
      templateUrl: 'search.html',
      providers: [app.PersonService]
    })
    .Class({
      constructor: [app.PersonService, ng.router.Router,
        function(_personService, _router) {
          this.searchTerm = '';
          this.searchPrefix = "";
          this.people = [];
          this._personService = _personService;
          this._router = _router;
        }
      ],
      searchFor(term){
        if (term.length < 2)
          this.people = [];
        else {
          this._personService.getUsers(term)
            .then(function(people) {
              //Problem is with the 'this' below:
              this.people = people;
            });
        }
      }
    });
})(window.app || (window.app = {}));

I've had some success in making the components and services work but I'm a bit stumped on this , ha . 我已经在做的组件和服务方面的工作取得了一些成功,但我有点难倒this哈哈 Any help or corrections with the issue would really be appreciated! 对此问题的任何帮助或更正将不胜感激!

Change the code to be: 将代码更改为:

 this._personService.getUsers(term)
        .then(function(people) {
          //Problem is with the 'this' below:
          this.people = people;
        }.bind(this));

(The context of this is wrong. In ES6 you have arrow function which solve this problem) (这是错误的上下文。在ES6中,您具有解决此问题的箭头功能)

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

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