In my code, I have private variables in the constructor and public variables in the class.
To reference these variables and functions, I use the "this" keyword.
However, when trying to access these variables inside a function, I am getting an "undefined" error.
As a newcomer to Typescript, how can I access these variables within the function?
Technology: Typescript, Angular 2, Angular 1.6.5, JavaScript
admin-company-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);
});
}
I heard about using .bind(this) but I'm unsure where exactly to add it.
https://angular.io/guide/http for ref.