angular - How do I get a set "this" to a variable in a component in angular2 typescript? -


i have component. inside have following:

constructor() {   this.something = "hello"; }  document.addeventlistener('click', dosomething());  function dosomething(e) {    console.log(this.something) // undefined } 

i want make can access this.something inside of dosomething(e). create variable outside , use var self = this; reference self.something "hello". unfortunately in angular 2, getting errors self not being defined, etc. how can access this.something inside of event listener created?

you should use lambda function correct this

document.addeventlistener('click', () => dosomething());


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -