Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2. Http. Subscribe: "this" pointer

Got:

http.request('js/app/config/config.json').subscribe(data => {
            this.url = data.json().url;
        });

and somehow "this" points to Subscriber. Don't know why... cause I thought that fat-arrow lambda will catch parent's class pointer.

Why so?

like image 692
korovaisdead Avatar asked Jan 12 '16 07:01

korovaisdead


1 Answers

Based on the screenshot : http://d.pr/i/iBa

enter image description here

You are debugging this in the console. Please note that this on the console will be the actual this. When TypeScript generates an arrow function for non ES6 JavaScript (which doesn't have native support for arrow functions) this is mapped to _this (and other things) which means that you need to view _this.

TIP

Just debug the generated JavaScript when learning TypeScript. This is the TypeScript bug : https://github.com/Microsoft/TypeScript/issues/2859 if you are interested.

like image 193
basarat Avatar answered Sep 23 '22 23:09

basarat