如何在事件侦听器中访问类成员的值?

loading...


0

在下面的示例中,我无法从 getSubDiv() 函数访问 hint 的值 . 如何访问 hint 的值?

export class UserComponent implements OnInit {    
  hint = "hi santu";

  constructor() { }

  ngOnInit() {    
  }

  creatediv() {   
    console.log("hello");    
    const maindiv = document.createElement("div");    
    const inp = document.createElement("input");    
    inp.setAttribute('type', 'button');    
    inp.setAttribute('value', 'Show');    
    maindiv.appendChild(inp);    
    // inp.type = "button";   
    // inp.value = "click me";    
    inp.onclick = this.getSubDiv;    
    const first = document.getElementById('first');    
    first.appendChild(maindiv);     
  }

  getSubDiv(e) {     
    const show_name = document.createElement('div');   
    show_name.innerText = "Santanu";    
    console.log(this.hint);    //here this.hint can't access
  }    
}

loading...

0回答

No Data

评论

暂时没有评论!