如何在此函数中访问此变量

spa*_*dev 0 javascript this typescript angular

我有private变量constructorpublic变量class.

我使用this关键字来引用这些变量和函数.

undefined如果我尝试在此函数中访问此变量,我会得到.

我是新手typescript,如何在此函数中访问此变量?

技术:Typescript,Angular 2,Angular 1.6.5,JavaScript

管理 - 公司settings.ts

import { Component } from '../../../../common/extentions/to-angular2';
import { Http } from '@angular/http';

export class AdminCompanySettings {
    public company: any;
    constructor(private $http: ng.IHttpService) {
        //
    }

    this.company = "New company";
    console.log("Prints all public variables", this); //prints all variables

    var data = { url: www.google.com, data: { user: value } }

    this.$http(data).then(function (response) {

        console.log(response);
        console.log(this.company); // undefined cannot access company
        console.log("Prints window object", this); //this will print window 
                                                   //and not company var or 
                                                   //other scope vars
    }).catch(function (error) {

        console.log(error);

    });
}
Run Code Online (Sandbox Code Playgroud)

我怀疑它可以被使用,.bind(this)但我不熟悉添加的位置.bind();

https://angular.io/guide/http for ref.

Rah*_*ngh 8

利用保留值的箭头功能 this

this.$http(data).then((response) => {
        console.log(response);
        console.log(this.company);
        console.log("Prints window object", this);
    }).catch(function (error) {
        console.log(error);
    });
Run Code Online (Sandbox Code Playgroud)