Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Angular 使用角度设置文本区域的焦点';s ElementRef:“引用;财产';setFocus';不存在于类型';ElementRef'&引用;_Angular_Typescript_Ionic Framework_Ionic3 - Fatal编程技术网

Angular 使用角度设置文本区域的焦点';s ElementRef:“引用;财产';setFocus';不存在于类型';ElementRef'&引用;

Angular 使用角度设置文本区域的焦点';s ElementRef:“引用;财产';setFocus';不存在于类型';ElementRef'&引用;,angular,typescript,ionic-framework,ionic3,Angular,Typescript,Ionic Framework,Ionic3,我已经创建了一个textarea组件,当它在ngAfterViewInit()方法中创建时,它会聚焦自身: 它工作得非常好,行为完全符合我的预期 我正在使用ElementRef获取ion textarea组件: @ViewChild('name') theinput: ElementRef; <ion-textarea #name rows="1" ></ion-textarea> 在this.theinput.setFocus()代码行中 如果我注释掉这行代码,构

我已经创建了一个textarea组件,当它在ngAfterViewInit()方法中创建时,它会聚焦自身:

它工作得非常好,行为完全符合我的预期

我正在使用ElementRef获取ion textarea组件:

@ViewChild('name') theinput: ElementRef;


<ion-textarea #name rows="1" ></ion-textarea>
this.theinput.setFocus()代码行中

如果我注释掉这行代码,构建应用程序,然后取消注释-一切都按预期进行。然而,这不是一个好的解决办法

对于这样的问题,有更好的解决方法吗?扩展ElementRef或类似的东西?

试试这个

使用ViewChild装饰器获取dom元素

@ViewChild('ref') ref:TextInput;
然后使用nativeElement,它是dom中的textarea,其中包括聚焦方法

ngAfterViewInit() {
    if(this.text.length===0){
    this.ref['_native'].nativeElement.focus();
    }
  }

示例:https:

好的,这很有效。然而,你能简单解释一下这个解决方案吗?这不被认为是一种不好的做法吗?这不是一种不好的做法,因为我们正在使用ViewChild Decorator触发nativeElement方法this.ref.native.nativeElement.focus();
@ViewChild('ref') ref:TextInput;
ngAfterViewInit() {
    if(this.text.length===0){
    this.ref['_native'].nativeElement.focus();
    }
  }