angular - How to reference Angular2 component properties/methods without 'this' keyword? -


i building application on angular2 , have been running problem 'this' keyword keeps giving me trouble. since in components (properties, methods) properties on class, if want reference them within method, have use 'this' keyword, lose reference because nested far multiple functions. use closures lot fix problem (shown below) closures don't work because closure function looses reference method uses inside of (like call service). example:

export class mycomponent implements oninit {      private data;      getinfofromservice() {         this.data = this.myservice.fetchallinfo();     }      retrieveinformation() {         var getinfo = this.getinfofromservice;         settimeout(function() {             getinfo();         }, 5000);     }      ngoninit() {         this.retrieveinformation();     }  }     

basically getting @ is, there anyway reference method on class without 'this' keyword? example, write like:

   retrieveinformation() {         settimeout(function() {             mycomponent.getinfofromservice();  // instead of this.getinfofromservice()         }, 5000);     } 

i know syntax mycomponent.getinfofromservice() doesn't work, there that do?


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -