Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过id获取元素-Angular2_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 通过id获取元素-Angular2

Javascript 通过id获取元素-Angular2,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个密码: document.getElementById('loginInput').value = '123'; 但在编译代码时,我收到以下错误: 类型HtmleElement上不存在属性值 我已经声明了一个var:value:string 如何避免这个错误 谢谢。如果您想设置值,您可以在单击或某些事件触发时在某些功能中执行相同的操作 您还可以使用ViewChild像这样使用局部变量来获取值 <input type='text' id='loginInput' #abc/>

我有一个密码:

document.getElementById('loginInput').value = '123';
但在编译代码时,我收到以下错误:

类型HtmleElement上不存在属性值

我已经声明了一个var:
value:string

如何避免这个错误


谢谢。

如果您想设置值,您可以在单击或某些事件触发时在某些功能中执行相同的操作

您还可以使用
ViewChild
像这样使用局部变量来获取值

<input type='text' id='loginInput' #abc/>
this.abc.nativeElement.value
ngAfterViewInit(){
    document.getElementById('loginInput').value = '123344565';
  }

更新 好的,明白了,你必须使用angualr2的
ngAfterViewInit
方法来处理类似的问题

<input type='text' id='loginInput' #abc/>
this.abc.nativeElement.value
ngAfterViewInit(){
    document.getElementById('loginInput').value = '123344565';
  }
ngAfterViewInit
不会抛出任何错误,因为它将在加载模板后呈现


另一种方法,也就是说:你可以用“角度法”,使用
ngModel
并跳过
document.getElementById('loginInput')。value='123'全部。相反:

<input type="text" [(ngModel)]="username"/>
<input type="text" [(ngModel)]="password"/>
这将在导航到页面时预设用户名和密码。

(document.getElementById('loginInput'))。值='123';
(<HTMLInputElement>document.getElementById('loginInput')).value = '123';
Angular无法直接获取HTML元素,因此需要通过将上述泛型绑定到元素来指定元素类型

更新::

这也可以使用ViewChild和#localvariable来完成,如中所述

{{todo.note}

从'@angular/core'导入{ElementRef,Renderer2};
@ViewChild('someVar')el:ElementRef;
构造函数(私有rd:Renderer2){}
ngAfterViewInit(){
console.log(this.rd);
this.el.nativeElement.focus();//平面角度方式(通过Id设置/获取值):

//在Html标记中

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

似乎您确实需要id为的组件,而不是id为的元素。但这不受支持。请提供som详细信息code@GünterZöchbauer那么我怎样才能捕捉到这个元素呢?
?如果你使用angular 2,你可以使用它的数据绑定功能,但不要这样做,只需遵循angular.io关于绑定的教程……这里有很多例子,如果我是否要将外部javascript文件中的值传递到angular component表单字段?这就是我所做的,当算法生成完值时,javascript文件中有一个算法,它使用以下代码getDocumentById('input')传递回angular表单.value=someValue;。但是,在OnSubmit()事件中尝试检索该值时我得到了未定义的值。有想法吗?很有趣。这在angular 6中是真的吗?是的。angular 6也是真的。这是angular 6中的首选方法吗?它在angular 7中也有效,但我不确定这是否是angular 7中的方法。显然不是@skydev,但根据问题要求,我回答了这个问题
    export class testUserClass {
       @ViewChild('userNameId') userNameId: ElementRef;

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

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



   }