- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 textarea 组件,当它在 ngAfterViewInit() 方法中创建时,它会聚焦自身:
ngAfterViewInit() {
if(this.text.length===0){
this.theinput.setFocus();
}
}
它工作得很好,行为完全符合我的预期。
我正在使用 ElementRef 获取 ion-textarea 组件:
@ViewChild('name') theinput: ElementRef;
<ion-textarea #name rows="1" ></ion-textarea>
但是,当运行 ionic serve
并构建应用程序时,它不会构建并显示错误:
"Property 'setFocus' does not exist on type 'ElementRef'."
在 this.theinput.setFocus()
代码行。
如果我注释掉这行代码,构建应用程序,然后取消注释 - 一切都按预期进行。然而,这不是一个好的解决方案。
对于这样的问题有更好的解决方法吗?扩展 ElementRef 或类似的东西?
最佳答案
试试这个
使用ViewChild装饰器获取dom元素
@ViewChild('ref') ref:TextInput;
然后使用 nativeElement,它是 dom 中的文本区域,其中包含 focus 方法。
ngAfterViewInit() {
if(this.text.length===0){
this.ref['_native'].nativeElement.focus();
}
}
关于angular - 使用 Angular 的 ElementRef : "Property ' setFocus' does not exist on type 'ElementRef' . 设置 ionic 文本区域的焦点“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51882397/
我创建了一个 textarea 组件,当它在 ngAfterViewInit() 方法中创建时,它会聚焦自身: ngAfterViewInit() { if(this.text.length=
我想为接收 ElementRef 的方法编写测试。我找不到模拟 ElementRef 的方法,有人可以帮忙吗?非常感谢! 示例服务.service.ts: export class exampleSe
我正在使用 angular 6 应用程序。我有一个 textarea,我想在页面加载后立即将焦点放在上面。我不能这样做。 这是页面的样子: Back to Administratio
我可以在组件构造函数中访问 ElementRef: export class MyComponent { constructor(element: ElementRef) { e
我正在尝试实现 malarkey作为 Angular2 管道。这个想法是你将能够编写一个表达式,如 ['foo','bar','baz'] | malarkey 并获得打字效果。 Malarkey 需
我有 ParentComponent 和 ChildComponent。父组件使用 ng-content 因此它可以在任何类型的 DOM 元素层次结构中包含任何内容。 我的子组件有一个特定的要求: W
在以前的angular版本中,我们可以像这样定义一个带有元素ref的viewchild @ViewChild('fileInput') fileInput: ElementRef; 现在我们必须在构造
我正在尝试通过单击按钮动态加载微调器组件。 Plunker 链接在这里, http://plnkr.co/edit/UEEQQRzX7RIkSO49rCHu?p=preview let element
在 Angular 中试验 ElementRef、TemplateRef、ViewRef 和 ViewContainerRef。我已经创建了一个 HTML 模板,我想从那里创建一个 View 并将该
我在 CommentComponent 中使用 ElementRef ,该组件导出到其他模块,如 ArticleModule、ProductModule 等... // CommentModule @
我需要在子节点中获取ElementRef。 我的模板 我调用 @ViewChildren('charts') infoCharts: ElementRef; 并获取 child for
我在组件中定义了字符串列表,它在 View 中呈现如下: {{item}} 单击每个项目后,我想在该项目之后添加 div 元素: clickedSpan.insertAdjacentHTML('be
我想在 Angular 2 中创建一个组件,我可以在其中从 HTML 传递值。我以为我会使用 ElementRef,但我似乎无法在没有错误的情况下引用它。 这是我的代码: import { Compo
我的组件中有以下构造函数: constructor (private el: ElementRef) {this.el = el.nativeElement} 然后在我的 ngOnInit 中: ng
感觉这应该是一个很基础的问题... 我正在使用一个返回不可见/不呈现的 DOM 节点/元素的插件。我能够轻松地将它转换为 ElementRef,并正确填充 nativeElement。 如何获取元素的
我正在尝试找出如何模拟注入(inject)到组件中的 ElementRef。我的组件如下: app.component.ts: import { Component, ElementRef } fro
我正在处理一个使用 Angular 5 中的 Material Component 的项目。我确实更新了我的 Visual 代码,但我不知道发生了什么。我面临“类型‘ElementRef’不是通用的”
我有以下设置: @Directive({ selector: '[test]' }) export class Test { constructor( private
如标题所示,我需要获取 routerLinkActive 的 ElementRef,以便了解我需要在正确位置放置“墨水栏”(例如 Material Design 选项卡)的位置。 这是我的sideNa
当我使用@ViewChild('serachField', { static: false }) serachField: ElementRef我得到错误 Property 'serachField'
我是一名优秀的程序员,十分优秀!