title: How to access class variable in callback in Typescript?
body: <p>Here is my current code:</p>
  
  <pre><code>import {Page} from 'ionic-angular';
  import {BLE} from 'ionic-native';
  
  @Page({
    templateUrl: 'build/pages/list/list.html'
  })
  export class ListPage { 
    devices: Array&lt;{name:string, id: string}&gt;;
  
    constructor() {  
      this.devices=[];       
    } 
    startScan (){
      this.devices = []; // This "this" exists and works fine
      BLE.scan([],5).subscribe(
        (device)=&gt;{        
          if(device.name){
            this.devices.push({name:device.name,id:device.id});  // this.devices does not exists
          }             
        },
        (err) =&gt; {
          console.log(JSON.stringify(err));
        }
        );
    }
  
    connectToDevice(device){
      BLE.connect(device.id).subscribe(success=&gt;{
         console.log(JSON.stringify(success));
      });
    }
  }
  </code></pre>
  
  <p>When calling startScan function I am trying to push returned device to array, however, this.devices is not available. I have tried saving this (self=this), but still with no luck. Can anyone help me to understand what i am missing?</p>
  
  <p>UPDATE:
  Setting </p>
  
  <pre><code>var self = this;
  </code></pre>
  
  <p>at the top of startScan() and then using it in .subscribe callback is the answer!</p>