简体   繁体   English

通过 id 获取元素 - Angular2

[英]Get element by id - Angular2

I have a code:我有一个代码:

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

But while compiling the code I receive following error:但是在编译代码时,我收到以下错误:

Property value does not exist on type HTMLElement. HTMLElement 类型上不存在属性值。

I have declared a var: value: string;我已经声明了一个 var: value: string; . .

How can I avoid this error?我怎样才能避免这个错误?

Thank you.谢谢你。

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您也可以使用像这样的局部变量使用ViewChild获得价值

<input type='text' id='loginInput' #abc/>

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方法

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

ngAfterViewInit will not throw any error because it will render after template loading ngAfterViewInit不会抛出任何错误,因为它会在模板加载后呈现

(<HTMLInputElement>document.getElementById('loginInput')).value = '123';

Angular cannot take HTML elements directly thereby you need to specify the element type by binding the above generic to it. Angular 不能直接获取 HTML 元素,因此您需要通过将上述泛型绑定到它来指定元素类型。

UPDATE::更新::

This can also be done using ViewChild with #localvariable as shown here, as mentioned in here这也可以使用ViewChild与#localvariable做如下图所示,在提到这里

<textarea  #someVar  id="tasknote"
                  name="tasknote"
                  [(ngModel)]="taskNote"
                  placeholder="{{ notePlaceholder }}"
                  style="background-color: pink"
                  (blur)="updateNote() ; noteEditMode = false " (click)="noteEditMode = false"> {{ todo.note }} 

</textarea>

import {ElementRef,Renderer2} from '@angular/core';
@ViewChild('someVar') el:ElementRef;

constructor(private rd: Renderer2) {}

ngAfterViewInit() {
      console.log(this.rd); 
      this.el.nativeElement.focus();      //<<<=====same as oldest way
}

A different approach, ie: You could just do it 'the Angular way' and use ngModel and skip document.getElementById('loginInput').value = '123';一种不同的方法,即:您可以使用“Angular 方式”并使用ngModel并跳过document.getElementById('loginInput').value = '123'; altogether.共。 Instead:反而:

<input type="text" [(ngModel)]="username"/>
<input type="text" [(ngModel)]="password"/>

and in your component you give these values:并在您的组件中提供以下值:

username: 'whatever'
password: 'whatever'

this will preset the username and password upon navigating to page.这将在导航到页面时预设用户名和密码。

Complate Angular Way ( Set/Get value by Id ): Complate Angular Way(通过 Id 设置/获取值):

// In Html tag // 在 Html 标签中

 <button (click) ="setValue()">Set Value</button>
 <input type="text"  #userNameId />

// In component .ts File // 在组件 .ts 文件中

    export class testUserClass {
       @ViewChild('userNameId') userNameId: ElementRef;

      ngAfterViewInit(){
         console.log(this.userNameId.nativeElement.value );
       }

      setValue(){
        this.userNameId.nativeElement.value = "Sample user Name";
      }



   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM