Get element by id - Angular2

后端 未结 4 1233
一整个雨季
一整个雨季 2020-12-29 18:31

I have a code:

document.getElementById(\'loginInput\').value = \'123\';

But while compiling the code I receive following error:

4条回答
  •  抹茶落季
    2020-12-29 19:13

    if you want to set value than you can do the same in some function on click or on some event fire.

    also you can get value using ViewChild using local variable like this

    
    

    and get value like this

    this.abc.nativeElement.value
    

    here is working example

    Update

    okay got it , you have to use ngAfterViewInit method of angualr2 for the same like this

    ngAfterViewInit(){
        document.getElementById('loginInput').value = '123344565';
      }
    

    ngAfterViewInit will not throw any error because it will render after template loading

提交回复
热议问题