有人知道如何获取组件模板中定义的元素吗?聚合物使得它真正用简单的$和$$.
我只是想知道如何在Angular中实现它.
以教程为例:
import {Component} from '@angular/core'
@Component({
selector:'display'
template:`
<input #myname(input)="updateName(myname.value)"/>
<p>My name : {{myName}}</p>
`
})
export class DisplayComponent {
myName: string = "Aman";
updateName(input: String) {
this.myName = input;
}
}
Run Code Online (Sandbox Code Playgroud)
如何从类定义中捕获por input元素的引用?
在Angular2中,如何在元素焦点上设置绑定.我不想用elementRef设置它.我认为在AngularJS中有ngFocus指令在Angular2中没有这样的指令
我正在尝试在 Dialog 元素中使用 AutoCompete 组件。当 Diaglog 打开时,我希望焦点转到 AutoComplement 元素。
我还没有找到这方面的确切教程。
我的效果是基于这个stackoverflow链接: How to use Angular4 to set focus by element id
这个 Github 问题:https : //github.com/primefaces/primeng/issues/2029 虽然我不明白像 onAfterShow 事件这样的部分,但该线程中的一些人尝试过但不起作用。
我的代码是这样的(简化):
成分
<p-dialog [(visible)]="this.display" modal="true"(onShow)="this.onAfterShow($event)">
<p-autoComplete #autoCompleteObject>
</p-autoComplete>
<some-other-components>
<p-dialog>
Run Code Online (Sandbox Code Playgroud)
打字稿:
@ViewChild('autoCompleteObject') private autoCompleteObject: AutoComplete ;
onAfterShow(event){ this.autoCompleteObject.domHandler.findSingle(this.autoCompleteObject.el.nativeElement, 'input').focus();
}
Run Code Online (Sandbox Code Playgroud)
或者像这样:
@ViewChild('autoCompleteObject', {read: ElementRef}) private autoCompleteObject: ElementRef ;
onAfterShow(event){
console.log("diaglog shows");
this.autoCompleteObject.nativeElement.focus();
}
Run Code Online (Sandbox Code Playgroud)
当diaglog 打开时, onAfterShow() 函数执行没有错误。但是,焦点未设置在自动完成元素上。
我哪里出错了有什么建议吗?先感谢您。